/*
 * WiFi账号、密码输入
 * EZOpenSDK
 *
 * Created by junecheng on 2024/5/13.
 * Copyright (c) 2024年 Ezviz. All rights reserved.
 */
import GlobalKit from '../../global/GlobalKit'
import EZToastUtil from '../../utils/EZToastUtil'
import RouterManager from '../../global/RouterManager/RouterManager'

import { NavigationView } from '../../global/view/NavigationView'
import SheetDialog from '../../global/view/SheetDialog'

import EZWiFiInfoRouterParam from './EZAPWiFiConfigPage'

@Entry
@Component
struct EZWifiInfoPage {
  // 是否支持5G频段配网
  @State supportWiFi5G: boolean = false
  // wifi名称
  @State ssid: string = ''
  // wifi密码
  @State password: string = ''
  // 配网方式
  @State configModeArray: Array<ResourceStr> = []
  configModeActionArray: Array<() => void> = []
  // 配网方式选择 弹框控制器
  private configModeController: CustomDialogController = new CustomDialogController({
    customStyle: true,
    alignment: DialogAlignment.Bottom,
    builder: SheetDialog({
      headerTitle: $r('app.string.wifi_mode_select_msg'),
      titles: this.configModeArray,
      confirmClick: (index) => {
        this.configModeActionArray[index]()
      }
    }),
    autoCancel: true,
    offset: { dx: 0, dy: -25 }
  })

  // ------------------------ 生命周期 ------------------------

  aboutToAppear() {
    const routerParams = RouterManager.getParams() as Record<string, boolean>
    this.supportWiFi5G = routerParams.supportWiFi5G as boolean
    if (routerParams.supportApMode as boolean) { // 支持AP配网
      this.configModeArray.push($r('app.string.wifi_mode_ap'))
      this.configModeActionArray.push(() => {
        RouterManager.pushUrl(RouterManager.apWiFiConfigPage, new EZWiFiInfoRouterParam(this.ssid, this.password, true))
      })
    }
    this.configModeArray.push($r('app.string.wifi_mode_line'))
    this.configModeActionArray.push(() => {

    })

    if (GlobalKit.isJCTest) {
      this.ssid = 'ezviz_mobile_AV'
      this.password = 'chengjun7'
    }
  }

  aboutToDisappear() {

  }

  // ------------------------ Actions ------------------------

  // ------------------------ 网络请求 ------------------------

  // ------------------------ 自定义方法 ------------------------

  // ------------------------ UI ------------------------

  build() {
    Column() {
      NavigationView({
        title: $r('app.string.wifi_connect_wifi_title')
      })
      Stack({ alignContent: Alignment.Top }) {
        Image($r('app.media.wifi_bg'))
        Column() {
          Text($r('app.string.wifi_connect_wifi_tip'))
            .margin({ left: 12, right: 12, top: 20 })
            .textAlign(TextAlign.Center)
            .visibility(this.supportWiFi5G ? Visibility.Hidden : Visibility.Visible)
          TextInput({ text: this.ssid, placeholder: $r('app.string.wifi_connect_wifi_name') })
            .onChange((value: string) => {
              this.ssid = value
            })
            .backgroundColor(Color.White)
            .borderRadius(0)
            .fontSize(14)
            .placeholderFont({ size: 14 })
            .padding({ left: 10 })
            .height(40)
            .margin({ top: 24 })
          TextInput({ text: this.password, placeholder: $r('app.string.wifi_connect_wifi_password') })
            .onChange((value: string) => {
              this.password = value
            })
            .backgroundColor(Color.White)
            .borderRadius(0)
            .fontSize(14)
            .placeholderFont({ size: 14 })
            .padding({ left: 10 })
            .height(40)
            .margin({ top: 24 })
          Button($r('app.string.ad_input_manual_next'))
            .width('80%')
            .height(40)
            .margin({ top: 31 })
            .backgroundColor($r('app.color.ez_text_blue'))
            .onClick(() => {
              if (!this.supportWiFi5G && this.ssid.includes('5G')) {
                EZToastUtil.showToast($r('app.string.wifi_connect_wifi_tip'))
                return
              }
              this.configModeController.open()
            })
        }
      }
    }
  }
}