import User from "../../FrameWork/User/User";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import Common5 from "../../Platform/th/Common5";
import LevelUpManager from "../Manager/LevelUpManager";
import UserManager from "../Manager/UserManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";


const { ccclass, property } = cc._decorator;

@ccclass
export default class WcPage extends cc.Component {


    prefabs: cc.Node = null;
    content: cc.Node = null;
    time: cc.Node = null;
    count: cc.Node = null;
    btnNv: cc.Node = null;
    btnNan: cc.Node = null;
    money: cc.Node = null;
    moneyContent: cc.Node = null;
    text1: cc.Node = null;
    text2: cc.Node = null;
    roles: cc.Node = null;
    end: cc.Node = null;
    center: cc.Node = null;
    allMoney: cc.Node = null;
    allCount: cc.Node = null;
    maxCount: cc.Node = null;
    btnGet: cc.Node = null;
    btnVideo: cc.Node = null;
    btnBack: cc.Node = null;
    isfail: boolean = false;

    protected onLoad(): void {
        this.prefabs = this.node.getChildByName("prefabs");
        this.content = this.node.getChildByName("content");
        this.time = this.node.getChildByName("time");
        this.count = this.node.getChildByName("count");
        this.btnNv = this.node.getChildByName("btnNv");
        this.btnNan = this.node.getChildByName("btnNan");
        this.money = this.node.getChildByName("money");
        this.moneyContent = this.node.getChildByName("moneyContent");
        this.text1 = this.node.getChildByName("text1");
        this.text2 = this.node.getChildByName("text2");
        this.roles = this.content.getChildByName("roles");
        this.end = this.node.getChildByName("end");
        this.center = this.end.getChildByName("center");
        this.allMoney = this.center.getChildByName("allMoney");
        this.allCount = this.center.getChildByName("allCount");
        this.maxCount = this.center.getChildByName("maxCount");
        this.btnGet = this.center.getChildByName("btnGet");
        this.btnVideo = this.center.getChildByName("btnVideo");
        this.btnBack = this.node.getChildByName("btnBack");

        this.btnNv.on(cc.Node.EventType.TOUCH_END, this.onClickNv, this);
        this.btnNan.on(cc.Node.EventType.TOUCH_END, this.onClickNan, this);
        this.btnGet.on(cc.Node.EventType.TOUCH_END, this.onClickbtnGet, this);
        this.btnVideo.on(cc.Node.EventType.TOUCH_END, this.onClickbtnVideo, this);
        this.btnBack.on(cc.Node.EventType.TOUCH_END, this.onClickbtnBack, this);

        this.initRole();
    }

    private _time = 0;
    private _timer = null;
    private _count = 0;
    private _errCount = 0;
    private MAX_ERR_COUNT = 5;
    private _curentRole: cc.Node = null;
    private _curentType = 0;
    private _maxCount = 0;

    initRole() {
        for (let i = 0; i < 7; i++) {
            this.scheduleOnce(this.createRole.bind(this), i * 0.1);
        }
        this.startGame();
    }

    startGame() {
        this._time = 60;
        this._errCount = 3;
        this._maxCount = User.getStatistics("wcc");
        this._timer = setInterval(() => {
            if (this._time == 0)
                this.endGame();
            this.updateUI();
            this._time--;
            if (this._time < 0)
                this._time = 0;
        }, 1000);
    }

    endGame(isfail?) {
        Common5.showInterstitialAd()
        this.isfail = true
        clearInterval(this._timer);
        this.node.getChildByName("firstNode").active = true
        let levelUpSpned = LevelUpManager.levelUpSpned()
        let yibei = levelUpSpned
        let wubei = levelUpSpned * 5
        if (isfail) {
            yibei /= 10
        }
        if (isfail) {
            wubei /= 10
        }
        this.node.getChildByName("firstNode").getChildByName("lab1").getComponent(cc.Label).string = '' + Common5.getNumberChangeHanzi(yibei, '1', 1)
        this.node.getChildByName("firstNode").getChildByName("lab2").getComponent(cc.Label).string = '' + Common5.getNumberChangeHanzi(wubei, '1', 1)
        // this.end.active = true;
        // this.center.scale = 0;
        // cc.tween(this.center).to(0.1, { scale: 1.2 }).to(0.15, { scale: 1 }).start();
        // User.setStatistics("wcd", 300);
    }

    onClickNv() {
        Common5.playEffect("点击");
        this.select(2);
    }

    onClickNan() {
        Common5.playEffect("点击");
        this.select(1);
    }

    onClickbtnGet() {
        Common5.playEffect("点击");
        Common5.ReportDY("inLevel", '普通领取');
        this.getReward();
    }

    onClickbtnVideo() {
        Common5.playEffect("点击");
        let tab = {
            onClose: (finish) => {
                if (finish) {
                    Common5.ReportDY("inLevel", '找厕所-AD-双倍领取');
                    this.getReward(5);
                }
                else {
                    Common5.showTips_custom("广告未观看完");
                }
            },
            onFailed: () => { }
        }
        AppPlatform.playVideo_custom(tab);
    }

    onClickbtnBack() {
        Common5.playEffect("点击");
        clearInterval(this._timer);
        this.node.destroy();
    }

    updateUI() {
        this.count.getComponent(cc.Label).string = this._errCount + "";
        this.time.getComponent(cc.Label).string = this._time + "";
        this.maxCount.getComponent(cc.Label).string = this._maxCount + "";
        this.allCount.getComponent(cc.Label).string = this._count + "";
        this.money.getComponent(cc.Label).string = this.reward() + "";
        this.allMoney.getComponentInChildren(cc.Label).string = " x " + this.reward();
    }

    select(type: number) {
        this._curentType = type;
        if (this._curentType == this.checkType(this._curentRole.name))
            this.selectRight();
        else
            this.selectErr();

        let tx = this._curentType == 1 ? -300 : 300;

        if (this._curentRole != null) {
            let r = this._curentRole;
            cc.tween(r).to(0.2, { opacity: 0 }).start();
            cc.tween(r).by(0.2, { position: cc.v3(tx, 50) }).call(() => {
                r.destroy();
            }).start();
        }
        this.removeCurent();

    }

    selectErr() {
        this._errCount--;
        let target = this._curentType == 1 ? this.text1 : this.text2;
        this.updateUI();
        if (this._errCount <= 0)
            this.endGame(true);
        cc.tween(target)
            .to(0.1, { scale: 1.1 })
            .to(0.1, { scale: 1 })
            .to(0.1, { scale: 1.1 })
            .to(0.1, { scale: 1 })
            .to(0.1, { scale: 1.1 })
            .to(0.1, { scale: 1 })
            .call(() => {
                target.scale = 0;
            }).start();
    }

    selectRight() {
        this._count++;
        this.updateUI();
        let m = cc.instantiate(this.prefabs.getChildByName("钱"));
        m.parent = this.moneyContent;
        m.active = true;
        m.position = cc.v3(0, -200);
        let tx = Math.random() * 120 - 70;
        let ty = Math.random() * 60 - 400;
        cc.tween(m).to(0.2, { position: cc.v3(tx, ty) }).start();
    }

    checkType(tname) {
        switch (tname) {
            case "主妇":
            case "女人":
            case "皇后": return 2;
            case "妃子":
            case "小孩":
            case "大金链":
            case "男人": return 1;
        }
    }

    createRole() {
        let skarr = this.prefabs.getComponentsInChildren(sp.Skeleton);
        if (this.roles.children.length != 0)
            skarr = skarr.filter(x => { return x.node.name != this.roles.children[0].name });
        let index = Math.floor(Math.random() * skarr.length);
        let n = cc.instantiate(skarr[index].node);
        n.setParent(this.roles);
        n.setSiblingIndex(0);
        this._curentRole = this.roles.children[this.roles.children.length - 1];
        this.updatePos();
    }

    removeCurent() {
        if (this._curentRole != null) {
            this._curentRole.parent = this.content;
        }
        this.createRole();
    }

    updatePos() {
        let x = 0;
        let y = -246.604;
        let scale = 1;

        let count = this.roles.children.length;
        for (let i = 0; i < count; i++) {
            let index = count - i - 1;
            let r = this.roles.children[index];
            let tx = x + i * (20 - i * 2);
            if (i % 2 == 0) tx = tx - 70
            else tx = tx + 70;
            if (i == 0) tx = 0;
            let ty = y + i * 60;
            let tscale = scale - 0.1 * i;
            cc.tween(r).to(0.2, { x: tx }).start();
            cc.tween(r).to(0.2, { y: ty }).start();
            cc.tween(r).to(0.2, { scale: tscale }).start();
        }

    }

    private getReward(beiShu = 1) {
        let levelUpSpned = LevelUpManager.levelUpSpned()

        // if (this._maxCount < this._count)
        //     User.setStatistics("wcc", this._count);
        let bei = levelUpSpned * beiShu
        if (this.isfail) {
            bei /= 10
        }
        UserManager.addMoney(bei, this.node);
        this.node.destroy();
    }

    private reward() {
        // let levelData = GameDataManager.getLevelData(User.getCurentStall());
        // return this._count * levelData.wcMoney;
        let levelUpSpned = LevelUpManager.levelUpSpned()
        return '' + Common5.getNumberChangeHanzi(levelUpSpned, '1', 1)
    }
}