import { advertising, AutoAdComponent } from '@kit.AdsKit';
import { BannerAd } from '../../ads/BannerAd';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { AdStatus } from '../../constants';

@Component
export struct ShowBannerAd {
  @State visibilityState: Visibility = Visibility.Hidden;
  private bannerAd = new BannerAd()

  build() {
    Column() {
      this.buildBannerView()
    }
    .borderRadius(4)
    // .backgroundColor($r('app.color.main_green'))
    .height(54)
    .width('100%')
    .clip(true)
  }

  @Builder
  buildBannerView() {
    Column() {
      AutoAdComponent({
        adParam: this.bannerAd.adParam,
        adOptions: this.bannerAd.adOptions,
        displayOptions: this.bannerAd.adDisplayOptions,
        interactionListener: {
          onStatusChanged: (status: string, ad: advertising.Advertisement, data: string) => {
            hilog.info(0x0000, 'testTag', '%{public}s', `status is ${status}`);
            switch (status) {
              case AdStatus.AD_OPEN:
                hilog.info(0x0000, 'testTag', '%{public}s', 'Status is onAdOpen');
                break;
              case AdStatus.AD_CLICKED:
                hilog.info(0x0000, 'testTag', '%{public}s', 'Status is onAdClick');
                break;
              case AdStatus.AD_CLOSED:
                hilog.info(0x0000, 'testTag', '%{public}s', 'Status is onAdClose');
                this.visibilityState = Visibility.None;
                break;
              case AdStatus.AD_LOAD:
                hilog.info(0x0000, 'testTag', '%{public}s', 'Status is onAdLoad');
                this.visibilityState = Visibility.Visible;
                break;
              case AdStatus.AD_FAIL:
                hilog.error(0x0000, 'testTag', '%{public}s', 'Status is onAdFail');
                this.visibilityState = Visibility.None;
                break;
            }
          }
        }
      })
    }
    .width('100%')
    .aspectRatio(this.bannerAd.ratio)
    .visibility(this.visibilityState)
  }
}