import { BasicType, IBestToast, userInfo, UserInfo, YTAvoid, YTLog, yTRouter } from "basic"
import { common, Want } from "@kit.AbilityKit"
import { BusinessError } from "@kit.BasicServicesKit"
import { BUNDLE_NAME } from "BuildProfile"
import { systemShare } from "@kit.ShareKit"
import { uniformTypeDescriptor } from "@kit.ArkData"
import { promptAction } from "@kit.ArkUI"


@Builder
function MineBuilder() {
  NavDestination() {
    Mine()
  }.title("关于我们")
  .hideTitleBar(true)
}

@Component
export struct Mine {
  @StorageProp(YTAvoid.SAFE_TOP_KEY) safeTop: number = 0
  @StorageProp(UserInfo.KEY) userInfo: UserInfo = userInfo
  @State isAutoBackImg:boolean=false
  setArr: Array<BasicType<undefined>> = [
    {
      acSrc:$r('app.media.sharefri'),
      text: '分享给好友',
      click: () => {

        this.shareData()


      },
      src: $r('app.media.rightxx')
    },
    {
      acSrc:$r('app.media.sugestion'),
      text: '意见反馈',
      click: () => {
        if (this.userInfo.checkLogin()) {
          yTRouter.router2SuggestionPage()
        } else {
          yTRouter.router2LoginPage()
        }
      },
      src: $r('app.media.rightxx')
    },
    // {
    //   acSrc:$r('app.media.sharefri'),
    //   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.rightxx')
    // },
    {
      acSrc:$r('app.media.abountwo'),
      text: '关于我们',
      click: () => {
        yTRouter.router2AboutUS()
      },
      src: $r('app.media.rightxx')
    },
    {
      acSrc:$r('app.media.backzhanghao'),
      text: '退出账号',
      click: () => {
        this.userInfo.logout()
        IBestToast.show({ message: '退出登录成功' })

      },
      src: $r('app.media.rightxx')
    }
  ]

  @State private isShow:boolean=false

  async  shareData() {
    try {
      // 构造ShareData，需配置一条有效数据信息
      const data = new systemShare.SharedData({
        // utd: uniformTypeDescriptor.UniformDataType.HYPERLINK,
        utd:uniformTypeDescriptor.UniformDataType.TEXT,
        title: '樱桃小决定',
        content:'http://appgallery.huawei.com/app/detail?id=6917580107917312127',
        description:"http://appgallery.huawei.com/app/detail?id=6917580107917312127"
        // content: 'store://appgallery.huawei.com/app/detail?id=6917580107917312127',
        // thumbnail:new Uint8Array(buffer),
        // thumbnailUri:"https://imgtool.net/cdn/image/2023/20230401_1.jpg"
      })
      const controller = new systemShare.ShareController(data)
      const context = getContext() as common.UIAbilityContext;
      // 进行分享面板显示
      controller.show(context, {
        previewMode: systemShare.SharePreviewMode.DEFAULT,
        selectionMode: systemShare.SelectionMode.SINGLE
      })
    }
    catch (error) {
      promptAction.showToast({ message: '当前设备不支持分享' })
    }
  }

  build() {
    Column() {
      Row(){

        Text('我的').fontSize(18).fontWeight(700).fontColor('#000')


      }.width('100%').justifyContent(FlexAlign.Center)
      .height(44 + this.safeTop)
      .padding({ top: this.safeTop ,left:10,right:10})
      .margin({bottom:40})

      Column() {
        Column() {
          //头像
          Image(this.userInfo.getHeadImg() ?? $r("app.media.app_icon"))
            .aspectRatio(1)
            .width(80)
            .height(80)
            .borderRadius(40)
            .margin({bottom:10 })

          Text(this.userInfo.getName() ?? this.userInfo.getPhoneNumber() ?? this.userInfo.getId()?.toString() ??
            '未登录')
            .fontSize(18)
            .fontColor('#FF1C1C1C')
            .fontWeight(500)
          // 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.Center)
        .onClick(() => {
          if (this.userInfo.checkLogin()) {
            yTRouter.router2SettingPage()
            return
          }
          yTRouter.router2LoginPage()
        })
        Text('用户设置')
          .fontSize(14)
          .padding({left:14,right:14,top:4,bottom:4})
          .fontColor('#707376')
          .border({width:1,color:'#c6cacf'})
          .borderRadius(15)
          .margin({top:8})  //（68，32  ）
          .onClick(()=>{
            if (this.userInfo.checkLogin()) {
              yTRouter.router2SettingPage()
              return
            }
            IBestToast.show({
              message:"请登录"
            })
            yTRouter.router2LoginPage()

          })

      }
      .padding({left:13,top:4})
      .width('100%')
      .margin({ bottom: 30 })


      Column(){
        // ShowBannerAd()
        Column() {

          List({space:5}) {
            ForEach(this.setArr, (item: BasicType<undefined>, index) => {
              ListItem() {
                Row() {
                  Row({space:10}) {
                    Image(item.acSrc).width(24)
                    Text(item.text)
                      .fontWeight(500).fontColor('#FF1C1C1C')
                  }
                  // if (index==0) {
                  //   // Row() {
                  //   //   Text('去分享').fontSize(14).fontColor('#801C1C1C')
                  //   //   // Text(this.userInfo.getAiNum()?.toString() ?? '')
                  //   //   //   .fontWeight(600)
                  //   //   //   .fontSize($r('[basic].float.page_text_font_size_14'))
                  //   //   // Image($r('app.media.rightxx'))
                  //   //   //   .width(24)
                  //   //   //   .aspectRatio(1)
                  //   // }
                  // }
                  // else {
                    Image($r('app.media.rightxx'))
                      .width(24)
                      .aspectRatio(1)
                  // }
                }
                .width('100%')
                .backgroundColor(Color.White)
                .height(56)
                .padding({left:10,right:10})
                .justifyContent(FlexAlign.SpaceBetween)
                .onClick(() => {
                  item.click?.()
                })
              }
            })
          }
          // .divider({ strokeWidth: 1, color: '#1A1C1C1C'})
          .width('100%')
        }
        .padding({left:8,right:8})  //24=16+8
        .layoutWeight(1)
        .width('100%')
      }.width('100%')
      .backgroundColor('#f7f7f7')
      .borderRadius({topLeft:20,topRight:20})
      .layoutWeight(1)
      .padding({ left: 16, right: 16 })

    }.width('100%')
    .height('100%')
    .backgroundColor(Color.Black)
    .backgroundImage($r('[basic].media.backimgNumber'))
    .backgroundImageSize({width:'100%',height:"100%"})

  }
}