import LocalStorage from "../../Data/LocalStorage";
import MainScreen from "../Screen/MainScreen";
import UIManager from "../UIManager";
import ItemSkin from "./ItemSkin";
import PopupUI, { PopupName } from "./PopupUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ShopSkin extends PopupUI {

    @property(MainScreen)
    mainScreen: MainScreen = null;

    @property([cc.SpriteFrame])
    listSkin: cc.SpriteFrame[] = [];

    @property(cc.Sprite)
    showSkin: cc.Sprite = null;

    @property(cc.Prefab)
    itemSkin: cc.Prefab = null;

    @property(cc.Node)
    containerSkin: cc.Node = null;

    @property(cc.Node)
    btnEquip: cc.Node = null;

    @property(cc.Node)
    btnBuy: cc.Node = null;

    @property(cc.Label)
    txtEquip: cc.Label = null;

    listItemWorm: ItemSkin[] = [];

    @property
    addNumberGem: number = 20;

    @property
    subNumberGem: number = 200;

    currentIndex: number = 0;
    currentIndexRandom: number = 0;
    listWormLock = [];
    listRandomLock = [];
    idClick: number = 0;

    override initialize(manager: UIManager): void {
        super.initialize(manager);
        this.popupName = PopupName.SHOPSKIN;
        this.showShopSkin();
    }

    showShopSkin() {
        for (let index: number = 0; index < this.listSkin.length; index++) {
            let skin: cc.Node = cc.instantiate(this.itemSkin);
            this.containerSkin.addChild(skin);
            let itemSkin = skin.getComponent(ItemSkin);
            this.listItemWorm.push(itemSkin);
            itemSkin.updateSkin({
                id: index,
                skin: this.listSkin[index],
                isLock: LocalStorage.getWormActive().includes(index),
                isSelected: LocalStorage.getWormSelected() == index ? true : false
            }, this);
        }
    }

    disableSelected() {
        for (let i = 0; i < this.listItemWorm.length; i++) {
            this.listItemWorm[i].onActiveSelected(false);
        }
    }

    disableChecked() {
        for (let i = 0; i < this.listItemWorm.length; i++) {
            if (this.listItemWorm[i].isSlected) {
                this.listItemWorm[i].isSlected = false;
                this.listItemWorm[i].onActiveChecked(false);
                this.listItemWorm[i].onActiveSelected(false);
                return;
            }
        }
    }

    onAddGem() {
        this.mainScreen.countGem(this.addNumberGem);
    }

    onWormRandom() {
        if (LocalStorage.getGem() < this.subNumberGem) {
            this.uiManager.showMessage("Not enough gem");
            return;
        }
        this.listWormLock = [];
        this.listRandomLock = [];
        this.currentIndex = 0;
        this.mainScreen.countGem(- this.subNumberGem);
        for (let index: number = 0; index < this.listSkin.length; index++) {
            if (!LocalStorage.getWormActive().includes(index)) {
                this.listWormLock.push(index);
            }
        }

        const numberOfElementsToAdd = 8;
        const newArray: number[] = [];
        let fakeArray: number[] = [];
        for (let i = 0; i < numberOfElementsToAdd; i++) {
            const index = i % this.listWormLock.length;
            newArray.push(this.listWormLock[index]);
        }
        fakeArray = this.listWormLock;
        this.listWormLock = newArray;
        this.activateNextWormLock();

        this.scheduleOnce(() => {
            this.listRandomLock = fakeArray;
            this.listRandomLock = this.shuffleArray(this.listWormLock);
            this.activateRandomWormLock();
        }, numberOfElementsToAdd * 0.2);

    }

    activateNextWormLock() {
        if (this.currentIndex < this.listWormLock.length) {
            if (this.currentIndex > 0) {
                this.listItemWorm[this.listWormLock[this.currentIndex - 1]].onActiveSelected(false);
            }
            this.listItemWorm[this.listWormLock[this.currentIndex]].onActiveSelected(true);
            this.currentIndex++;
            this.scheduleOnce(() => {
                this.activateNextWormLock();
            }, 0.2);
        }
        else if (this.currentIndex == this.listWormLock.length) {
            this.listItemWorm[this.listItemWorm.length - 1].onActiveSelected(false);
        }
    }

    activateRandomWormLock() {
        if (this.currentIndexRandom < this.listRandomLock.length) {
            if (this.currentIndexRandom > 0) {
                this.listItemWorm[this.listRandomLock[this.currentIndexRandom - 1]].onActiveSelected(false);
            }
            this.listItemWorm[this.listRandomLock[this.currentIndexRandom]].onActiveSelected(true);
            this.currentIndexRandom++;
            this.scheduleOnce(() => {
                this.activateRandomWormLock();
            }, 0.2);
        } else if (this.currentIndexRandom == this.listRandomLock.length) {
            this.listItemWorm[this.listRandomLock[this.currentIndexRandom - 1]].onActiveSelected(false);
            let random = Math.round(this.getRandomNumber(this.listWormLock[0], this.listWormLock.length));
            console.log(random);
            this.listItemWorm[random].openWorm();
        }
    }

    shuffleArray<T>(array: T[]): T[] {
        const shuffledArray = [...array];
        for (let i = shuffledArray.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
        }
        return shuffledArray;
    }

    getRandomNumber(min, max) {
        return Math.random() * (max - min) + min;
    }

    scaleWorm(){
        this.showSkin.node.scale = 1.2;
        cc.tween(this.showSkin.node)
        .to(0.3,{scale: 1})
        .start();
    }

    hide(): void {
        super.hide();
        this.mainScreen.showListWorm();
    }

    show(): void {
        super.show();
    }

    onClickEquipped(){
        for (let i = 0; i < this.listItemWorm.length; i++) {
            if (this.listItemWorm[i].id == this.idClick) {
                this.listItemWorm[i].openWorm();
            }            
        }
    }
}
