import { find, Node, _decorator } from 'cc';
import { BaseLayer } from '../../common/BaseLayer';
import { cocosUtil } from '../../utils/cocosUtil';
import { utilTools } from '../../utils/utilTools';
import { constants } from '../data/constants';
import { localText } from '../data/localText';
import { msgac } from '../data/msgac';
import { ItemLayer } from '../item/ItemLayer';
import { audioManager } from '../manager/audioManager';
import { designManager } from '../manager/designManager';
import { eventManager } from '../manager/eventManager';
import { iconManager } from '../manager/iconManager';
import { levelManager } from '../manager/levelManager';
import { sdkManager } from '../manager/sdkManager';
import { playerModel } from '../model/playerModel';
const { ccclass, property } = _decorator;

@ccclass('ShopLayer')
export class ShopLayer extends BaseLayer {

    coinNode: Node;

    titleNode1: Node;
    titleNode2: Node;

    shopLayer: Node;
    btnCoinAdd: Node;

    type: number;
    id: number;

    onLoad() {
        super.onLoad();

        this.coinNode = this.getNodeByPath("coinLayer/num");

        this.titleNode1 = this.getNodeByPath("info/title1");
        this.titleNode2 = this.getNodeByPath("info/title2");

        this.shopLayer = this.getNodeByPath("info/shopLayer");
        this.shopLayer.addComponent(ItemLayer);
        this.btnCoinAdd = this.getNodeByPath("btns/btnCoinAdd");

        this.type = this.obj.type;
        this.id = this.obj.id;

        this.initUI();

        sdkManager.instance.hideCustomAdCommon();
    }

    onEnable() {
        this.addEventArr([
            msgac.coinChange,
        ]);
    }

    onDisable() {
        this.removeEventArr([
            msgac.coinChange,
        ]);
    }

    initUI() {
        this.initLocaltext();
        this.initEffect();

        this.refreshCoinLayer();

        if (this.type == constants.shopTypes.secret) {
            this.titleNode2.active = true;
            this.titleNode1.active = false;
        } else {
            this.titleNode1.active = true;
            this.titleNode2.active = false;
        }

        let arr = this.getShopArr();
        this.shopLayer.getComponent(ItemLayer).initUI(this, arr, this.refreshShopItem.bind(this));
    }

    refreshCoinLayer() {
        this.setString(this.coinNode, playerModel.instance.getCoin());
    }

    refreshShopItem(itemUI, item, index) {
        let equipItem = find("equipItem", itemUI);
        let skillItem = find("skillItem", itemUI);
        equipItem.active = false;
        skillItem.active = false;
        let hasBuyNode = null;
        if (this.type == constants.shopTypes.equip || this.type == constants.shopTypes.secret) {
            equipItem.active = true;
            hasBuyNode = find("hasBuy", equipItem);

            this.setString(find("atk/val", equipItem), "+" + item.atk);
            let equipRow = designManager.instance.getRowById(constants.tableName.equip, item.equipId);
            iconManager.instance.setSprite(find("icon", equipItem), equipRow.icon);


        } else if (this.type == constants.shopTypes.skill) {
            skillItem.active = true;
            hasBuyNode = find("hasBuy", skillItem);

            let skillRow = designManager.instance.getRowById(constants.tableName.skill, item.skillId);
            iconManager.instance.setSprite(find("icon", skillItem), skillRow.icon);
            this.setString(find("info/info", skillItem), skillRow.info);
        }

        let btnBuy = find("btnBuy", itemUI);
        this.setString(find("price", btnBuy), -item.price);
        btnBuy["item"] = item;

        if (item.hasBuy) {
            btnBuy.active = false;
            hasBuyNode.active = true;
        } else {
            btnBuy.active = true;
            hasBuyNode.active = false;
        }
    }

    initEffect() {
        cocosUtil.tweenScaleBreath(this.btnCoinAdd);
    }

    initLocaltext() {

    }

    getShopArr() {
        let arr = [];
        if (this.type == constants.shopTypes.equip) {
            // 装备商店
            let row = designManager.instance.getRowById(constants.tableName.shop_equip, this.id);
            for (let i in row.idArr) {
                let id = row.idArr[i];
                let info: any = {};
                info.equipId = id;
                info.price = row.priceArr[i];
                info.atk = row.atkArr[i];
                arr.push(info);
            }
        } else if (this.type == constants.shopTypes.secret) {
            // 神秘商店
            let tb = designManager.instance.getTable(constants.tableName.shop_secret);
            arr = utilTools.getRowsByWeightAndNum(tb, 3);
        } else if (this.type == constants.shopTypes.skill) {
            // 技能商店
            let tb = designManager.instance.getTable(constants.tableName.shop_skill);
            let tmpArr = [];
            for (let i in tb) {
                let row = tb[i];
                if (levelManager.instance.level % 2 == 0) {
                    if (row.skillId == 1 || row.skillId == 3) {
                        tmpArr.push(row);
                    }
                    continue;
                }
                if (row.skillId == 2 || row.skillId == 4) {
                    tmpArr.push(row);
                }
            }
            tb = tmpArr;
            arr = utilTools.getRowsByWeightAndNum(tb, 3);
        }

        return arr;
    };

    coinChangeRet(data: any) {
        this.refreshCoinLayer();
    }

    process_event(event: any) {
        let ac = event.ac;
        let data = event.data;
        switch (ac) {
            case msgac.coinChange:
                this.coinChangeRet(data);
                break;
            default:
                break;
        }
    }

    onButtonClick(node: Node, name: string) {
        switch (name) {
            case "btnBuy":
                this.onClickBtnBuy(node);
                break;
            case "btnCoinAdd":
                this.onClickBtnCoinAdd(node);
                break;
            case "btnNext":
                this.onClickBtnNext(node);
                break;
            default:
                break;
        }
    }

    onClickBtnBuy(node: Node) {
        let item = node["item"];
        if (playerModel.instance.getCoin() < item.price) {
            this.createNotice(localText.textObj.lackCoin);
            return;
        }

        item.hasBuy = true;
        playerModel.instance.subCoin(item.price);
        this.shopLayer.getComponent(ItemLayer).refreshUI();

        if (this.type == constants.shopTypes.equip || this.type == constants.shopTypes.secret) {
            levelManager.instance.addAtk(item.atk);
            levelManager.instance.addEquip(item.equipId);
            audioManager.instance.playEffect(constants.audioNames.equip);
            node["hasPlayAudio"] = true;
        } else if (this.type == constants.shopTypes.skill) {
            playerModel.instance.addSkillNumById(item.skillId);
            eventManager.instance.send(msgac.skillLayerRefresh);
        }
    }

    onClickBtnNext(node: Node) {
        this.closeLayer();
        sdkManager.instance.showCustomAdCommon();
    }

    onClickBtnCoinAdd(node: Node) {
        sdkManager.instance.sendEvent("观看激励视频-领取金币500");
        sdkManager.instance.openAd((st: number) => {
            if (st != 1) {
                return;
            }
            this.btnCoinAdd.active = false;
            playerModel.instance.addCoin(levelManager.instance.shopAdCoin);
            this.openCoinGetEffectLayer(node);
            sdkManager.instance.sendEvent("观看完激励视频-领取金币500");
        });
    }

}

