import { UIManager } from "../models/UIManager";
import { StorageManager } from "../models/StorageManager";
import { Utility_ConvertSecondToString } from "../utils/utility";
import { respath } from "../constants/respath";
import { Difficulty } from "./Difficulty";
import { config } from "../constants/config";
import { DOStage } from "../models/DOStage";

const { regClass, property } = Laya;

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

    @property(Laya.Label)
    public label_diffucuty: Laya.Label
    @property(Laya.Label)
    public label_time: Laya.Label
    @property(Laya.Label)
    public label_score: Laya.Label

    @property(Laya.Box)
    public btn_new: Laya.Box
    @property(Laya.Box)
    public btn_home: Laya.Box
    @property(Laya.Box)
    public btn_continue: Laya.Box


    onStart(): void {

    }

    public onSetStageInfo(doStage: DOStage): void {
      this.label_diffucuty.text = doStage.get_difficulty()
      this.label_time.text = Utility_ConvertSecondToString(doStage.get_duration())
      this.label_score.text = doStage.get_score().toString()

      this.btn_new.on(Laya.Event.CLICK, this, (evt: Laya.Event) => {
        if (config.H_SCREEN) {
            var user = StorageManager.getInstance().getUser()
            user.update_progress(doStage.get_difficulty(), doStage.get_stageIndex()+1)//主线关卡更新难度进度
            StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, doStage.get_difficulty(), doStage.get_difficulty())
            UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID);
            this.owner.destroy()
        }
        else {
          Laya.loader.load(respath.difficulty_ui_res).then((go)=>{
            var prefab = go.create()
            var d = UIManager.getInstance().getUIRoot().addChild(prefab).getComponent(Difficulty)
            d.onInit((value: string)=> {
              console.log("选择难度", value)
              StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, value, value)
              UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID);
              this.owner.destroy()
            })
          })
        }
      })

      this.btn_continue.on(Laya.Event.CLICK, this, (evt: Laya.Event) => {//这个按钮只有从DC来的会显示
        UIManager.getInstance().loadDCUI()
        UIManager.getInstance().closeStageUI();
        this.owner.destroy()
      })

      this.btn_home.on(Laya.Event.CLICK, this, (evt: Laya.Event) => {
        if (config.H_SCREEN) {
          UIManager.getInstance().closeDCUI()
          if (doStage.get_stageType() == config.STAGE_TYPE.MAIN) {
            var user = StorageManager.getInstance().getUser()
            user.update_progress(doStage.get_difficulty(), doStage.get_stageIndex()+1)//主线关卡更新难度进度
            StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, doStage.get_difficulty(), doStage.get_difficulty())
            UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID);
          }
          else if (doStage.get_stageType() == config.STAGE_TYPE.DC) {
            UIManager.getInstance().setTopbarTo(true)
          }
        }
        else {
          UIManager.getInstance().loadHomeUI()
          UIManager.getInstance().closeStageUI();
        }
        this.owner.destroy()
      })

    }
}