import { Color, Label, Sprite, SpriteFrame, _decorator, CCInteger } from "cc";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { CCComp } from "../../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { SingletonModuleComp } from "../../common/SingletonModuleComp";
import FlashSprite from "../../../../../extensions/oops-plugin-framework/assets/libs/animator-effect/2d/FlashSprite";
import { VehicleType } from "../../vehicle/model/VehicleEnum";

const { ccclass, property } = _decorator;

/** 等级显示Item界面 */
@ccclass('CheckpointLevelItemViewComp')
@ecs.register('CheckpointLevelItemView', false)
export class CheckpointLevelItemViewComp extends CCComp {

    @property({ type: CCInteger, visible: true, tooltip: '当前关卡' })
    lv: number = 0

    private img_doubt
    private img_air
    private img_bus
    private img_car
    private img_lock
    private img_pass
    private img_line
    private label_level

    onAdded(args: any) {
        console.log(args);
    }

    onLoad() {
        this.img_air = this.node.getChildByPath("aircraft")
        this.img_bus = this.node.getChildByPath("Bus")
        this.img_car = this.node.getChildByPath("convertible")
        this.img_doubt = this.node.getChildByPath("doubt")
        this.img_lock = this.node.getChildByPath("Progressnotunlocked")
        this.img_pass = this.node.getChildByPath("Progressilluminated")
        this.img_line = this.node.getChildByPath("SpriteSplash")
        this.label_level = this.node.getChildByPath("level")
    }

    initView() {
        this.hideVechicle()
        this.img_pass.active = false
        this.img_doubt.active = false
        this.setTextLv()

    }

    setTextLv() {
        this.label_level.getComponent(Label).string = `${this.lv ? this.lv : ''}`

    }


    protected lateUpdate(dt: number): void {
        if (this.lv === 0) {
            this.initView()
            return
        }
        this.hideVechicle()
        this.setTextLv()
        const curLv = ecs.getSingleton(SingletonModuleComp).account.AccountModel.lv
        if (curLv >= this.lv) {
            this.img_pass.active = true
            this.img_lock.active = false
            this.changeProgressStatu(true)
        } else {
            this.img_pass.active = false
            this.img_lock.active = true
            this.changeProgressStatu(false)
            if (this.lv === curLv + 1) {
                const type = ecs.getSingleton(SingletonModuleComp).account.checkpoint.CheckpointModelLevel.rtluNext.vehicleType
                this.showVechicle(type)
                this.img_doubt.active = false
            } else {
                this.img_doubt.active = true
            }

        }
    }

    hideVechicle() {
        this.img_air.active = false
        this.img_car.active = false
        this.img_bus.active = false
        this.img_doubt.active = false
    }

    showVechicle(t: VehicleType) {
        this.hideVechicle()
        switch (t) {
            case VehicleType.CAR:
                this.img_car.active = true
                break;
            case VehicleType.BUS:
                this.img_bus.active = true
                break;
            case VehicleType.AIRPLANT:
                this.img_air.active = true
                break;

            default:
                this.img_doubt.active = false
                break;
        }
    }



    changeProgressStatu(v: boolean) {
        this.img_line.getComponent(Sprite).color = v ? new Color('#00B6F8') : new Color('#646464')
    }





    reset() {
        this.node.destroy();
    }
}