import {
  BasicType,
  copyText,
  IBestToast,
  userInfo,
  UserInfo,
  YTAvoid,
  YTLog,
  yTRouter
} from 'basic'
import { common, Want } from '@kit.AbilityKit'
import { BusinessError } from '@kit.BasicServicesKit'
import { BUNDLE_NAME } from 'BuildProfile'

@Component
export struct Mine {
  @StorageProp(YTAvoid.SAFE_TOP_KEY) safeTop: number = 0
  @StorageProp(UserInfo.KEY) userInfo: UserInfo = userInfo
  setArr: Array<BasicType<undefined>> = [
    {
      text: '意见反馈',
      click: () => {
        if (this.userInfo.checkLogin()) {
          yTRouter.router2SuggestionPage()
        } else {
          yTRouter.router2LoginPage()
        }
      },
      src: $r('app.media.right_arrow')
    },
    {
      text: '给个好评',
      click: () => {
        const want: Want = {
          uri: "store://appgallery.huawei.com/app/detail?id=" + BUNDLE_NAME
        };
        const context = this.getUIContext().getHostContext() as common.UIAbilityContext;
        context.startAbility(want).then(() => {
          //拉起成功
          YTLog.info('跳转成功')
        }).catch((err: BusinessError) => {
          // 拉起失败
          YTLog.error(err)
          IBestToast.show('出现未知错误,请稍后再试')
        });
      },
      src: $r('app.media.right_arrow')
    },
    {
      text: '关于我们',
      click: () => {
        yTRouter.router2AboutUS()
      },
      src: $r('app.media.right_arrow')
    }
  ]

  aboutToAppear(): void {

  }

  build() {
    Column() {


      Column() {
        Row() {
          Row() {
            Image(this.userInfo.getHeadImg() ?? $r('app.media.app_icon'))
              .aspectRatio(1)
              .height(40)
              .borderRadius(20)
              .margin({ right: 9 })

            Column({ space: 7 }) {
              Text(this.userInfo.getName() ?? this.userInfo.getPhoneNumber() ?? this.userInfo.getId()?.toString() ??
                '未登录')
                .fontSize($r('[basic].float.page_text_font_size_16'))
                .fontColor('Color.Black')
              Text() {
                Span('ID：' + (this.userInfo.getId()?.toString().padStart(8, '0') ?? '00000000'))
                ImageSpan($r('app.media.copy'))
                  .width(7)
                  .height(8)
                  .margin({ left: 4 })
                  .onClick(() => {
                    copyText((this.userInfo.getId()?.toString().padStart(8, '0') ?? '00000000'))
                  })
                  .offset({ bottom: 4 })
              }
              .fontColor('#80000000')
              .fontSize($r('[basic].float.page_text_font_size_10'))
            }
            .alignItems(HorizontalAlign.Start)
          }
          .onClick(() => {

            if (this.userInfo.checkLogin()) {
              yTRouter.router2SettingPage()
              return
            }
            yTRouter.router2LoginPage()
          })
        }
        .width('100%')
        .margin({ bottom: 30 })

        // ShowBannerAd()
        Column() {
          List() {
            ForEach(this.setArr, (item: BasicType<undefined>, index) => {
              ListItem() {
                Row() {
                  Text(item.text)
                    .fontColor('#80000000')
                    .fontSize(12)
                  if (!index) {
                    Row() {
                      Text(this.userInfo.getAiNum()?.toString() ?? '')
                        .fontWeight(600)
                        .fontSize($r('[basic].float.page_text_font_size_14'))
                      Image($r('app.media.right_arrow'))
                        .width(24)
                        .aspectRatio(1)
                    }
                  } else {
                    Image($r('app.media.right_arrow'))
                      .width(24)
                      .aspectRatio(1)
                  }
                }
                .width('100%')
                .height(36)
                .justifyContent(FlexAlign.SpaceBetween)
                .onClick(() => {
                  item.click?.()
                })
              }
            })
          }
          .padding({ left: 12, right: 4 })
          .divider({ strokeWidth: 1, color: '#0A000000', endMargin: 8 })
          .margin({ top: 30 })
          .width('100%')
          .height('100%')
        }
        .layoutWeight(1)
        .width('100%')

      }
      .padding({ top: this.safeTop + 22, left: 16, right: 16 })
      .height('100%')
    }.backgroundImage($r('[basic].media.back'))
    .backgroundImageSize({width:'100%',height:'100%'})
  }
}