import LocalStorage from "../../Data/LocalStorage";
import { PopupName } from "../Popup/PopupUI";
import UIManager from "../UIManager";
import ScreenUI, { ScreenName } from "./ScreenUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MainScreen extends ScreenUI {

    @property(cc.Node)
    logo: cc.Node = null;
    @property([cc.Prefab])
    listWormPrefab: cc.Prefab[] = [];
    @property(cc.Node)
    containerSkins: cc.Node = null;
    @property(cc.Label)
    txtGem: cc.Label = null;
    @property(cc.Label)
    txtPlayLevel: cc.Label = null;
    @property(cc.Button)
    btnTry: cc.Button = null;

    isMove: boolean = true;
    listWorm: { id: number, nodeWorm: cc.Node }[] = [];
    currentIndex: number = 0;
    moveDuration: number = 0.5;
    wormId: number = 0;
    isFullSkin: boolean = false;

    override initialize(uiManager: UIManager): void {
        super.initialize(uiManager);
        this.screenName = ScreenName.MAINMENU;
        this.initializeListWorm();
    }

    initializeListWorm() {
        this.listWorm = [];
        this.containerSkins.removeAllChildren();
        this.currentIndex = 0;
        let listWormActive: [number] = LocalStorage.getWormActive();
        if (listWormActive.length == this.listWormPrefab.length) {
            this.isFullSkin = true;
            this.btnTry.interactable = false;
            for (let index = 0; index < this.listWormPrefab.length; index++) {
                    let worm: cc.Node = cc.instantiate(this.listWormPrefab[index]);
                    this.containerSkins.addChild(worm);
                    if (this.containerSkins.childrenCount == 0) {
                        worm.position = cc.v3(0, 0);
                    } else {
                        worm.position = cc.v3(450);
                    }
                    this.listWorm.push({
                        id: index,
                        nodeWorm: worm
                    });
            }
        } else {
            for (let index = 0; index < this.listWormPrefab.length; index++) {
                if (!listWormActive.includes(index)) {
                    let worm: cc.Node = cc.instantiate(this.listWormPrefab[index]);
                    this.containerSkins.addChild(worm);
                    if (this.containerSkins.childrenCount == 0) {
                        worm.position = cc.v3(0, 0);
                    } else {
                        worm.position = cc.v3(450);
                    }
                    this.listWorm.push({
                        id: index,
                        nodeWorm: worm
                    });
                }
            }
        }

    }

    showListWorm() {
        if (this.isFullSkin) {
            return;
        }
        let listWormTemp: { id: number, nodeWorm: cc.Node }[] = [];
        let listWormActive = LocalStorage.getWormActive();
        for (let i = 0; i < this.listWorm.length; i++) {
            if (!listWormActive.includes(this.listWorm[i].id)) {
                listWormTemp.push(this.listWorm[i]);
            } else {
                this.listWorm[i].nodeWorm.active = false;
            }
        }
        this.listWorm = listWormTemp;
        this.currentIndex = 0;
        for (let index = 0; index < this.listWorm.length; index++) {
            if (index == 0) {
                this.listWorm[index].nodeWorm.position = cc.v3(0, 0);
            } else {
                this.listWorm[index].nodeWorm.position = cc.v3(450);
            }
        }
    }

    btnNextWorm() {
        if (this.currentIndex < this.listWorm.length - 1 && this.isMove) {
            this.currentIndex++;
            this.moveItems(true);
        }
    }

    btnPreWorm() {
        if (this.currentIndex > 0 && this.isMove) {
            this.currentIndex--;
            this.moveItems(false);
        }
    }

    moveItems(isNext: boolean) {
        this.isMove = false;
        cc.tween(this.listWorm[this.currentIndex].nodeWorm)
            .to(this.moveDuration, { position: cc.v3(0, 0), scale: 1.2 })
            .call(() => {
                this.isMove = true;
            })
            .start();
        if (isNext) {
            cc.tween(this.listWorm[this.currentIndex - 1].nodeWorm)
                .to(this.moveDuration, { position: cc.v3(-450, 0), scale: 1 })
                .start();

        } else {
            cc.tween(this.listWorm[this.currentIndex + 1].nodeWorm)
                .to(this.moveDuration, { position: cc.v3(450, 0), scale: 1 })
                .start();
        }
    }

    override active(): void {
        super.active();
        this.showListWorm();
        this.updateBtnPlay();
        this.updateGem();
        cc.tween(this.logo)
            .to(0.3, { position: cc.v3(0, 500) }, { easing: "bounceOut" })
            .start();
    }

    override deactive(): void {
        super.deactive();
        this.logo.position = cc.v3(0, 1132);
    }

    onClickRemoveAds() {
        this.uiManager.showMessage("Is Comming");
    }

    onClickLevel() {
        this.uiManager.activeScreen(ScreenName.LEVEL);
    }

    onClickShopSkin() {
        this.uiManager.showPopup(PopupName.SHOPSKIN);
    }

    updateGem() {
        this.txtGem.string = LocalStorage.getGem() + "";
    }

    countGem(gem: number){
        let fakeCurrentGem = LocalStorage.getGem();
        let fakseGemEnd = fakeCurrentGem + gem;
        LocalStorage.setGem(gem + fakeCurrentGem);        
        let start = fakeCurrentGem;
        let end = fakseGemEnd;
        cc.tween({ progress: 0 })
            .to(0.3, { progress: 1 }, {
                onUpdate: (target, ratio) => {
                    let end2 = Math.round(start + (end - start) * ratio);
                    this.txtGem.getComponent(cc.Label).string = "" + end2;
                }
            })
            .start();
    }

    updateBtnPlay() {
        this.txtPlayLevel.string = "Level " + (LocalStorage.getLevel() + 1) + ""
    }

    onClickPlay() {
    }

    onClickLuckySpin() {
        this.uiManager.activeScreen(ScreenName.LUCKYSPIN);
    }

    onClickTryWorm() {
        let worm = this.listWorm[this.currentIndex].nodeWorm.name;
        let id = '';
        if (worm) {
            id = worm.split("_")[1];
        }
        this.uiManager.gameManager.wormTry = Number(id);
    }
}
