import LocalStorage from "../../Data/LocalStorage";
import UIManager from "../UIManager";
import NodePage from "./Level/NodePage";
import Page from "./Level/Page";
import ScreenUI, { ScreenName } from "./ScreenUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LevelScreen extends ScreenUI {

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

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

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

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

    totalLevels: number = 0;
    levelPerPage: number = 12;
    curentLevel: number = 0;
    totalPages: number = 0;
    currentPage: number = 0;

    targetPos: cc.Vec3 = new cc.Vec3(0, 0, 0);
    @property(cc.Vec3)
    pageStep: cc.Vec3 = new cc.Vec3(850, 0, 0);
    tweenTime: number = 0.5;



    listNodePage: NodePage[] = []
    listPage: Page[] = []

    isActive: boolean = false;
    isMove: boolean = true;

    override initialize(uiManager: UIManager): void {
        super.initialize(uiManager);
        this.screenName = ScreenName.LEVEL;
        this.targetPos = this.containerPage.position;
        this.curentLevel = LocalStorage.getLevel();
        this.showListLevel();
    }

    showListLevel() {
        this.node.active = false;
        this.totalLevels = this.uiManager.gameManager.dataLevels.length;
        this.totalPages = Math.ceil(this.totalLevels / this.levelPerPage);
        for (let index = 0; index < this.totalPages; index++) {
            let nodePage: cc.Node = cc.instantiate(this.nodePage);
            let page: cc.Node = cc.instantiate(this.pagePrefab);
            this.containerPage.addChild(page);
            this.containerNodePage.addChild(nodePage);
            let currentPage = index;
            let start = currentPage * this.levelPerPage + 1;
            let end = start + this.levelPerPage;
            let p = page.getComponent(Page)
            p.initialize(start, end, this.levelPerPage);
            this.listPage.push(p)
            let np = nodePage.getComponent(NodePage)
            if (index == 0) {
                np.activeNode();
            }
            this.listNodePage.push(np);
        }
    }

    nextPage() {
        if (this.isMove) {
            this.isMove = false;
            if (this.currentPage < this.totalPages) {
                this.currentPage++;
                this.targetPos = cc.v3(this.targetPos.x + this.pageStep.x, this.targetPos.y + this.pageStep.y);
                this.movePage(this.targetPos);
                this.updateNodePage(true);
            }
        }
    }

    prePage() {
        if (this.isMove) {
            this.isMove = false;
            if (this.currentPage > 0) {
                this.currentPage--;
                this.targetPos = cc.v3(this.targetPos.x - this.pageStep.x, this.targetPos.y - this.pageStep.y);
                this.movePage(this.targetPos);
                this.updateNodePage(false);
            }
        }
    }

    movePage(targetPos ) {
        cc.tween(this.containerPage)
            .to(this.tweenTime, { position: targetPos }, { easing: "quintOut" })
            .call(() => {
                this.isMove = true;
            })
            .start();
    }

    updateNodePage(isNext) {
        this.listNodePage[this.currentPage].activeNode();
        if (isNext) {
            this.listNodePage[this.currentPage - 1].deActiveNode();
        } else {
            this.listNodePage[this.currentPage + 1].deActiveNode();
        }
    }

    reloadNodePage() {
        this.currentPage = 0;
        for (let index = 0; index < this.listPage.length; index++) {
            this.listPage[index].reload();
        }

        for (let i = 0; i < this.listNodePage.length; i++) {
            this.listNodePage[i].deActiveNode();
        }
        this.listNodePage[0].activeNode();
    }

    override active(): void {
        super.active();
        if (!this.isActive) {
            this.isActive = true;
        } else {
            this.reloadNodePage();
        }
    }

    override deactive(): void {
        super.deactive();
    }


}
