import { config } from "../constants/config"
import { StorageManager } from "../models/StorageManager"
import { UIManager } from "../models/UIManager"
import { CommonData } from "./common/CommonData"
import { G_ShowCommonTips } from "./CommonTips"

const { regClass, property } = Laya

@regClass()
export class TopBar extends Laya.Script {
  declare owner: Laya.Box

  @property(Laya.Box)
  public btn_classic: Laya.Box

  @property(Laya.Sprite)
  public sprite_classic: Laya.Sprite

  @property(Laya.Label)
  public label_classic: Laya.Label

  @property(Laya.Box)
  public btn_dc: Laya.Box

  @property(Laya.Sprite)
  public sprite_dc: Laya.Sprite

  @property(Laya.Label)
  public label_dc: Laya.Label

  private isClassic: boolean = true

  @property(Laya.Label)
  public obj_label: Laya.Label

  @property(Laya.Box)
  public obj_items: Laya.Box

  private items: Map<string, Laya.Label> = new Map()

  onStart(): void {
    // 如果是第一次以横屏启动游戏需要创建经典关卡
    let difficulty = Laya.LocalStorage.getItem("difficulty")// 读取用户上次选择的难度
    if (!difficulty || difficulty.length <= 0) {
      difficulty = config.DIFFICULTY_TYPE.Easy
      Laya.LocalStorage.setItem("difficulty", difficulty)
    }
    const doStage = StorageManager.getInstance().getStage(config.DEFAULT_STAGE_ID)
    if (!doStage) {
      StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, difficulty, difficulty)
    } else {
      const user = StorageManager.getInstance().getUser()
      if (user.get_done(config.DEFAULT_STAGE_ID) >= 1) {
        console.log("当前主线关卡完成")
        user.update_progress(doStage.get_difficulty(), doStage.get_stageIndex() + 1)// 主线关卡更新难度进度
        StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, difficulty, difficulty)
      } else if (doStage.get_mistake() >= config.MISTAKE_MAX) { // 上次失败了则重新开始
        StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, difficulty, difficulty)
      }
    }

    this.btn_classic.on(Laya.Event.CLICK, this, () => {
      if (this.isClassic === false) {
        this.onClickTab(true)
      }
    })
    this.btn_dc.on(Laya.Event.CLICK, this, () => {
      if (this.isClassic) {
        this.onClickTab(false)
      }
    })
    this.onClickTab(this.isClassic)

    const showToggle = Laya.LocalStorage.getItem("showToggle")
    if (!showToggle || showToggle.length <= 0) {
      Laya.LocalStorage.setItem("showToggle", "true")
    }

    for (let i = 0; i < this.obj_items.numChildren; i++) {
      const obj = this.obj_items.getChildAt(i)
      const value = config.DIFFICULTY_LIST[i]
      const label = obj.getChildByName("Label") as Laya.Label
      label.text = value
      this.items.set(value, label)
      const common = obj.getComponent(CommonData)
      common.strValue = value
      obj.on(Laya.Event.CLICK, this, (evt: Laya.Event) => {
        const common = evt.target.getComponent(CommonData)
        const difficulty = Laya.LocalStorage.getItem("difficulty")
        if (common.strValue !== difficulty) {
          if (Laya.LocalStorage.getItem("showToggle") === "true") {
            const title = "Start New Game"
            const content = "Current game progress will be lost"
            G_ShowCommonTips(title, content, true, (ok: boolean, toggleValue: boolean) => {
              if (ok) {
                if (toggleValue) {
                  Laya.LocalStorage.setItem("showToggle", "false")
                }
                this.onClickDifficulty(common.strValue)
              }
            })
          } else {
            this.onClickDifficulty(common.strValue)
          }
        }
      })
    }
    this.updateDifficulty(difficulty)
  }

  onClickTab(isClassic: boolean): void {
    this.isClassic = isClassic
    if (isClassic) {
      UIManager.getInstance().closeTrophyUI()
      UIManager.getInstance().closeDCUI()
      UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID)
    } else {
      UIManager.getInstance().closeStageUI()
      UIManager.getInstance().loadDCUI()
    }
    this.updateLeftTab()
  }

  updateLeftTab(): void {
    this.sprite_classic.alpha = this.isClassic ? 1 : 0.1
    this.label_classic.color = this.isClassic ? "#ffffff" : "#0e2a53"
    this.sprite_dc.alpha = this.isClassic ? 0.1 : 1
    this.label_dc.color = this.isClassic ? "#0e2a53" : "#ffffff"

    this.obj_label.visible = this.isClassic
    this.obj_items.visible = this.isClassic
  }

  onClickDifficulty(difficulty: string): void {
    Laya.LocalStorage.setItem("difficulty", difficulty)// 记录横屏模式用户选择的难度
    StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, difficulty, difficulty)
    UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID)

    this.updateDifficulty(difficulty)
  }

  updateDifficulty(difficulty: string): void {
    this.items.forEach((label: Laya.Label, key: string) => {
      label.color = "#0e2a53"
    })
    const label = this.items.get(difficulty)
    label.color = "#1d5cdc"
  }
}
