import { Color, find, Node, sp, Sprite, _decorator } from 'cc';
import { BaseLayer } from '../../common/BaseLayer';
import { utilTools } from '../../utils/utilTools';
import { constants } from '../data/constants';
import { ItemLayer } from '../item/ItemLayer';
import { designManager } from '../manager/designManager';
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('SkinLayer')
export class SkinLayer extends BaseLayer {

    roleBodySkeleon: sp.Skeleton;
    itemList: Node;

    btnGet: Node;

    onLoad() {
        super.onLoad();

        this.itemList = this.getNodeByPath("listBg/list");
        this.itemList.addComponent(ItemLayer);

        this.btnGet = this.getNodeByPath("btnGet");

        this.roleBodySkeleon = this.getNodeByPath("tribe/body").getComponent(sp.Skeleton);

        this.initUI();
    }

    initUI() {
        let tb = designManager.instance.getTable(constants.tableName.equip);
        this.itemList.getComponent(ItemLayer).initUI(this, tb, this.refreshEquipItem.bind(this));
        this.refreshRoleBodyLayer();
        this.refreshBtnGet();
    }

    refreshBtnGet() {
        this.btnGet.active = true;
        let tb = designManager.instance.getTable(constants.tableName.equip);
        for (let i in tb) {
            let row = tb[i];
            if (playerModel.instance.getEquipIdArr().indexOf(row.id) == -1) {
                return;
            }
        }
        this.btnGet.active = false;
    }

    refreshRoleBodyLayer() {
        let skinName = levelManager.instance.getSkinName(playerModel.instance.getEquipWeaponId(), playerModel.instance.getEquipDressId());
        this.roleBodySkeleon.setSkin(skinName);
    }

    refreshEquipItem(itemUI: Node, row: any) {
        let iconNode = find("icon", itemUI);
        iconManager.instance.setSprite(iconNode, row.icon);
        let getBg = find("getBg", itemUI);
        let noBg = find("noBg", itemUI);
        let wenhao = find("wenhao", itemUI);
        if (playerModel.instance.getEquipIdArr().indexOf(row.id) == -1) {
            // 还未获得
            noBg.active = true;
            getBg.active = false;
            wenhao.active = true;
            iconNode.getComponent(Sprite).color = Color.BLACK;
        } else {
            // 已获得
            getBg.active = true;
            noBg.active = false;
            wenhao.active = false;
            iconNode.getComponent(Sprite).color = Color.WHITE;
        }

        let choseWeapon = find("choseWeapon", itemUI);
        let choseDress = find("choseDress", itemUI);
        choseWeapon.active = false;
        choseDress.active = false;
        if (row.type == constants.equipTypes.weapon) {
            if (playerModel.instance.getEquipWeaponId() == row.id) {
                choseWeapon.active = true;
            }
        } else if (row.type == constants.equipTypes.dress) {
            if (playerModel.instance.getEquipDressId() == row.id) {
                choseDress.active = true;
            }
        }

    }

    onButtonClick(node: Node, name: string) {
        switch (name) {
            case "equipItem":
                this.onClickEquipItem(node);
                break;
            case "btnGet":
                this.onClickBtnGet(node);
                break;
            case "btnBack":
                this.onClickBtnBack(node);
                break;
            default:
                break;
        }
    }

    onClickEquipItem(node: Node) {
        let row = node["item"];
        if (playerModel.instance.getEquipIdArr().indexOf(row.id) == -1) {
            return;
        }
        if (row.type == constants.equipTypes.weapon) {
            playerModel.instance.setEquipWeaponId(row.id);
        } else if (row.type == constants.equipTypes.dress) {
            playerModel.instance.setEquipDressId(row.id);
        }
        this.itemList.getComponent(ItemLayer).refreshUI();
        this.refreshRoleBodyLayer();
    }

    onClickBtnGet(node: Node) {
        let tb = designManager.instance.getTable(constants.tableName.equip);
        let idArr = [];
        for (let i in tb) {
            let row = tb[i];
            if (playerModel.instance.getEquipIdArr().indexOf(row.id) != -1) {
                continue;
            }
            idArr.push(row.id);
        }
        if (idArr.length <= 0) {
            return;
        }

        sdkManager.instance.sendEvent("观看激励视频-解锁皮肤");
        sdkManager.instance.openAd((st: number) => {
            if (st != 1) {
                return;
            }
            let id = utilTools.getRandomItemByArr(idArr);
            playerModel.instance.addEquipId(id);
            this.itemList.getComponent(ItemLayer).refreshItemUIById(id);
            this.refreshBtnGet();
            sdkManager.instance.sendEvent("观看完激励视频-解锁皮肤");
        });

    }

    onClickBtnBack(node: Node) {
        this.closeLayer();
    }

}

