import { RouterUrls } from '../../common/RouterUrls';
import { ToastUtils } from '../../utils/ToastUtils';
import systemDateTime from '@ohos.systemDateTime';
import { router } from '@kit.ArkUI';
import { AppUtil } from '@pura/harmony-utils';
import { ConfigManager } from '../../manager/UserConfigManager';
import { EventReportManager } from '../../manager/EventReportManager';
import { EventConstants } from '../../common/EventConstants';

@Entry
@ComponentV2
struct GuidePage {
  child = [
    $r('app.media.ic_guide_1'),
    $r('app.media.ic_guide_2'),
    $r('app.media.ic_guide_3'),
    $r('app.media.ic_guide_4')
  ]
  currentIndex: number = 0;

  clickTime: number = 0;

  @Local showHomePage: boolean = true

  aboutToAppear(): void {
    EventReportManager.eventReport(EventConstants.GUIDE_LAUNCH)
  }

  build() {
    Stack() {
      Image($r('app.media.ic_guide_bg')).width('100%').height('100%')
      if (this.showHomePage) {
        Column() {
          Image($r('app.media.ic_guide_cover')).width('100%').aspectRatio(0.75)
          Text('视频搬运工具').fontColor('#0B2449').fontSize(32).fontFamily('almmsht')
            .margin({top: 27})
          Text(ConfigManager.getGuideHint()).fontColor('#1E385D').fontSize(16).margin({top: 12, left: 54, right: 54})
          Blank().layoutWeight(1)
          Button('立即使用')
            .width(180)
            .height(40)
            .fontColor(Color.White)
            .fontSize(16)
            .borderRadius(20)
            .backgroundColor($r("app.color.color_466afd"))
            .margin({bottom: 30})
            .onClick(() => {
              this.showHomePage = false
            })
        }
        .width('100%')
        .height('100%')
      } else {
        Swiper() {
          ForEach(this.child, (item: Resource) => {
            Image(item).width('100%').height('100%')
          })
        }
        .indicator(false)
        .onChange((index: number) => {
          this.currentIndex = index;
          EventReportManager.eventReport(EventConstants.GUIDE_OPPORTUNITY_SCROLL, `${index + 1}`)
        })
        .onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => {
          if (index === this.child.length - 1 && extraInfo.currentOffset < -60) {
            // 跳转VIP充值页面
            if (ConfigManager.isGuidePayEnable()) {
              this.getUIContext().getRouter().replaceUrl({ url: RouterUrls.VIP_PAGE, params: { origin: 'bootpage'} });
            } else {
              this.getUIContext().getRouter().replaceUrl({ url: RouterUrls.MAIN_PAGE }, router.RouterMode.Single);
            }
          }
        })
        .loop(false)
        .width('100%')
        .height('100%')
      }
    }
  }

  onBackPress(): boolean | void {
    if (systemDateTime.getTime() - this.clickTime < 1500) {
      AppUtil.getContext().terminateSelf();
    } else {
      this.clickTime = systemDateTime.getTime();
      ToastUtils.show('双击退出应用');
    }
    return true;
  }
}