import webview from '@ohos.web.webview';
import { HdNav,authStore,BasicConstant,Base64Util,DataWebModels,AESEncryptionDecryption } from '@itcast/basic';
import router from '@ohos.router';
import { BusinessError } from '@kit.BasicServicesKit';
import util from '@ohos.util';
@Entry
@Component
struct LivebroadcastPages {
  private controller: webview.WebviewController = new webview.WebviewController();
  @State params:DataWebModels = router.getParams() as DataWebModels;

  @State url: string = this.params.url;
  @State title: string = this.params.title;
  customUserAgent: string = 'gdxz-expert';
  @State plyvvparams:string=''

  aboutToAppear(): void {
    this.getUrl()
  }

  build() {
    Column() {
      HdNav({ title: this.title, showRightIcon: false, hasBorder: true })
      Web({
        src: this.url,
        controller: this.controller
      })
        .mixedMode(MixedMode.All)
        .domStorageAccess(true)
        .onControllerAttached(() => {
          let userAgent = this.controller.getUserAgent() + this.customUserAgent;
          this.controller.setCustomUserAgent(userAgent);
        })
        .javaScriptAccess(true)
        .onPageEnd(async() => {
          try {
            let ids:string  = authStore.getUser().uuid;
            let avatar:string = BasicConstant.urlHtml+authStore.getUser().photo
            let str:string =authStore.getUser().realName
            let name:string =await Base64Util.encodeToStr(AESEncryptionDecryption.stringToUint8Array(str), util.Type.BASIC);
            this.plyvvparams = "{\"name\":\""+name+"\",\"id\":\""+ids+"\",\"avatar\":\""+avatar+"\"}";
            this.controller.runJavaScript(
              "window.localStorage.setItem('watchlive_current_user','" +this.plyvvparams + "');",
              (error, result) => {
                if (error) {
                  console.error(`run JavaScript error, ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
                  return;
                }
                if (result) {

                  console.info(`The test() return value is: ${result}`);
                }
              });

          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })
        .width('100%')
        .layoutWeight(1)// 占据剩余空间
        .height('100%')
    }
    .height('100%')// 关键：约束父容器高度
  }

  async getUrl()
  {

    if('live'==this.params.type)
    {
      let ids:string  = authStore.getUser().uuid;
      let avatar:string = BasicConstant.urlHtml+authStore.getUser().photo
      let str:string =authStore.getUser().realName
      let name:string =await Base64Util.encodeToStr( AESEncryptionDecryption.stringToUint8Array(str), util.Type.BASIC);
      name.replaceAll("\r|\n","")
      console.info(`The test() return value is22: ${name+str}`);
      let signs =await AESEncryptionDecryption.aesEncrypt(ids+name+avatar+this.url,"deoep09_klodLdAo")
      let temurl=BasicConstant.zhibourl+"?id="+ids+"&name="+name+"&avatar="+avatar+"&url="+this.url+"&sign="+ signs;
      this.url=temurl
      console.info(`The test() return value is11: ${this.url}`);
    }

  }

  // async getUser()
  // {
  //   let ids:string  = authStore.getUser().uuid;
  //   let avatar:string = BasicConstant.urlHtml+authStore.getUser().photo
  //   let str:string =authStore.getUser().realName
  //   const encoder = new util.TextEncoder(); // 全局单例
  //   let name:string =await Base64Util.encodeToStr( encoder.encode(str), util.Type.BASIC);
  //   this.plyvvparams = "{\"name\":\""+name+"\",\"id\":\""+ids+"\",\"avatar\":\""+avatar+"\"}";
  // }
}

