import i18n from "../../../i18n/i18n";
import Main from "../../../main/Main";
import { AudioMgr } from "../../../main/ViewManage";
import { HttpStateType, ReveData } from "../../../util/CHttp";
import GoodItem from "../../common/GoodItem";
import PetIcon from "../../common/PetIcon";
import { __EquipData } from "../../data/sdata/SManage";
import { EquipAttr } from "../../data/udata/Player";
import Equip from "./Equip";
import EquipQH from "./EquipQH";

/**
 * 装备升星
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class EquipStar extends cc.Component {

    @property(GoodItem)
    mEquipItem: GoodItem = null;

    @property(cc.Node)
    mContent: cc.Node = null;//背包容器

    @property(cc.Prefab)
    mGoodItem: cc.Prefab = null;//道具节点

    @property(cc.Label)
    mEquipName: cc.Label = null;//装备名字

    @property(cc.Label)
    mBfbLabel: cc.Label = null;//成功百分比

    @property(cc.ProgressBar)
    mBfbBar: cc.ProgressBar = null;//成功百分比

    @property(cc.Label)
    mQHMoney: cc.Label = null;//强化消耗的货币

    @property(cc.Node)
    mXhContent: cc.Node = null;//消耗容器

    @property(cc.Node)
    mOneKeyBt: cc.Node = null;//一键放入按钮

    @property(cc.Label)
    mAttrLabel: cc.Label = null;//基础属性提升

    @property(cc.Node)
    mFullNode: cc.Node = null;//满星

    @property(sp.Skeleton)
    spine1: sp.Skeleton = null;

    @property(sp.Skeleton)
    spine2: sp.Skeleton = null;
    /**
     * 装备界面
     */
    public equip: Equip = null;
    /**
     * 当前选中的伙伴
     */
    private petIcon: PetIcon = null;
    /**
     * 当前强化的装备
     */
    // private goodItem: GoodItem = null;
    /**
    * 当前选中的道具
    */
    private curGoodItem: Array<GoodItem> = []

    private STAR_GL = [50, 30, 20, 10]
    private STAR_MONEY = [5000, 20000, 50000, 120000, 200000]

    private main: Main = null;
    public starType: number = 0;    // 0 人物装备升星  1 背包装备升星
    public equipAttr: EquipAttr = null;
    public equipData: __EquipData = null;
    onLoad() {
        this.mFullNode.active = false
        this.mBfbLabel.string = '0%'
        this.mBfbBar.progress = 0
    }
    public init(equip: Equip, petIcon: PetIcon, equipAttr: EquipAttr) {
        this.starType = 0;
        this.main = this.node.getComponent(EquipQH).main
        this.equip = equip
        this.petIcon = petIcon
        this.equipAttr = equipAttr
        this.equipData = this.main.sManage.getEquipById(equipAttr.id);

        for (let i = 0; i < 10; i++) {
            let node = cc.instantiate(this.mGoodItem);
            node.parent = this.mXhContent;
        }

        this.mQHMoney.string = '' + this.STAR_MONEY[this.equipAttr.star]

        this.flushEquipPag()
    }
    /**
     * 选择当前页卡
     */
    public checkToggle() {
        let scrollView = this.mContent.parent.parent.getComponent(cc.ScrollView)
        scrollView.stopAutoScroll()
        scrollView.scrollToTop()
        this.mOneKeyBt.active = true
        this.mEquipItem.initEquip(this.main, this.equipAttr)
        this.mEquipName.string = i18n.t(this.equipData.name)

        this.curGoodItem = []
        this.flushEquipPag();
        this.flushXH()
        let equipQH = this.node.getComponent(EquipQH)
        equipQH.setOneKeyCallback(() => {
            this.oneKeyPush()
        })
        equipQH.setSortCallback((type: number) => {
            if (type == 0) {//品质排序
                EquipQH.sortPag(0, this.mContent)
            } else {//战力排序
                EquipQH.sortPag(1, this.mContent)
            }
        })

    }
    /**
    * 刷新装备背包
    */
    public flushEquipPag() {
        let equip = this.main.player.equip;
        let index = 0;
        let content = this.mContent;
        for (let i = 0; i < equip.length; i++) {
            const element = equip[i];
            if (element.star == this.equipAttr.star) {
                if (this.equipAttr.__index == element.__index) {
                    this.starType = 1;
                } else {
                    let temp = this.main.sManage.getEquipById(element.id)
                    if (temp.type == this.equipData.type
                        && temp.pz == this.equipData.pz
                    ) {
                        if (index >= content.children.length) {
                            this.addGoodItem(content);
                        }
                        let node = content.children[index];
                        node.opacity = 255
                        let equipItem = node.getComponent(GoodItem);
                        equipItem.initEquip(this.main, element);
                        equipItem.setCallback((gi) => {
                            this.addEquip(gi);
                            this.main.playerEffectByPath(AudioMgr.qh2);
                        });
                        index++;
                    }
                }
            }
        }

        for (let i = index; i < content.children.length; i++) {
            let node = content.children[i];
            node.opacity = 255
            let equipItem = node.getComponent(GoodItem);
            equipItem.setNull();
        }

    }

    private addGoodItem(content: cc.Node) {
        for (let i = 0; i < 4; i++) {
            let node = cc.instantiate(this.mGoodItem);
            node.parent = content;
        }
    }

    /**
     * 背包中的装备添加到消耗材料
     * @param goodItem 
     */
    private addEquip(goodItem: GoodItem) {
        if (goodItem.node.opacity == 100) {
            return
        }
        if (this.curGoodItem.length >= 10) {
            this.main.showTips(i18n.t('消耗材料以满'))
        } else {
            goodItem.node.opacity = 100
            this.curGoodItem.push(goodItem)
            this.flushXH()
        }
    }

    private removeEquip(goodItem: GoodItem) {
        for (let i = 0; i < this.curGoodItem.length; i++) {
            const element = this.curGoodItem[i];
            if (element.equip.__index == goodItem.equip.__index) {
                element.node.opacity = 255
                this.curGoodItem.splice(i, 1)
                break
            }
        }
        this.flushXH()
    }

    private flushXH() {
        if (this.equipAttr.star >= 5) {
            this.mFullNode.active = true
            return
        }
        let exp = 0
        for (let i = 0; i < this.mXhContent.children.length; i++) {
            const node = this.mXhContent.children[i];
            let tmpItem = node.getComponent(GoodItem)
            if (i < this.curGoodItem.length) {
                let goodItem = this.curGoodItem[i]
                tmpItem.initEquip(this.main, goodItem.equip)
                tmpItem.setCallback((gItem: GoodItem) => {
                    this.removeEquip(gItem)
                })
                exp += this.STAR_GL[this.equipAttr.star]
            } else {
                tmpItem.setNull()
            }
        }
        if (exp >= 100) {
            exp = 100
            this.node.getComponent(EquipQH).mQHFull.active = true
            this.mAttrLabel.string = '基础属性提升+' + ((this.equipAttr.star + 1) * 10) + '%'
        } else {
            this.node.getComponent(EquipQH).mQHFull.active = false
            this.mAttrLabel.string = '基础属性提升+' + (this.equipAttr.star * 10) + '%'
        }
        this.mBfbLabel.string = exp + '%'
        this.mBfbBar.progress = exp / 100
        this.mQHMoney.string = '' + this.STAR_MONEY[this.equipAttr.star]

        if (this.curGoodItem.length >= 10) {
            this.mOneKeyBt.children[0].getComponent(cc.Label).string = i18n.t('一键卸下')
        } else {
            this.mOneKeyBt.children[0].getComponent(cc.Label).string = i18n.t('一键放入')
        }



    }
    /**
     * 一键放入
     */
    private oneKeyPush() {
        if (this.curGoodItem.length >= 10) {
            for (let i = 0; i < this.curGoodItem.length; i++) {
                const element = this.curGoodItem[i]
                element.node.opacity = 255
            }
            this.curGoodItem = []
        } else {
            for (let i = 0; i < this.curGoodItem.length; i++) {
                const element = this.curGoodItem[i]
                element.node.opacity = 255
            }
            this.curGoodItem = []
            let exp = 0
            for (let i = 0; i < 10; i++) {
                if (i >= this.mContent.children.length) {
                    break
                }
                const node = this.mContent.children[i];
                let equipItem = node.getComponent(GoodItem);
                if (equipItem.equip) {
                    equipItem.node.opacity = 100
                    exp += this.STAR_GL[this.equipAttr.star]
                    this.curGoodItem.push(equipItem)

                    if (exp >= 100) {
                        break
                    }
                }
            }
        }
        this.flushXH()
    }
    /**
     * 升星
     */
    public onclickStar() {
        if (this.starType == 0) {
            this.starFunc();
        } else if (this.starType == 1) {
            this.starFunc1();
        }
    }

    starFunc() {
        let removeEquips = []
        for (let i = 0; i < this.curGoodItem.length; i++) {
            const element = this.curGoodItem[i];
            removeEquips.push(element.equip.__index)
        }
        if(removeEquips.length == 0){
            this.main.showTips('没有添加任何材料');
            return
        }
        let msg = {
            petId: this.petIcon.id,
            equipId: this.equipAttr.id,
            equips: removeEquips
        }
        this.main.gameHttp.sendJson('equip/v1/star', msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    if (reve.data.star > 0) {
                        this.main.playerEffectByPath(AudioMgr.qh);
                        this.playQHSpine();
                        this.equipAttr.star = reve.data.star
                        this.main.showTips('升星成功');
                    } else {
                        this.main.showTips('升级失败');
                    }
                    this.curGoodItem = []
                    this.mEquipItem.flushEquip(this.main);
                    this.flushEquipPag()
                    this.equip.flushEquip(this.petIcon)
                    this.equip.flushPagSize()

                    this.flushXH()
                } else {
                    this.main.showTips(reve.message);
                }
            } else {
                this.main.showTips('网络异常');
            }
        });
    }

    starFunc1() {
        let removeEquips = []
        for (let i = 0; i < this.curGoodItem.length; i++) {
            const element = this.curGoodItem[i];
            removeEquips.push(element.equip.__index)
        }
        if(removeEquips.length == 0){
            this.main.showTips('没有添加任何材料');
            return
        }
        let msg = {
            equipIndex: this.equipAttr.__index,
            equips: removeEquips
        }
        this.main.gameHttp.sendJson('equip/v1/star1', msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    if (reve.data.star > 0) {
                        this.main.playerEffectByPath(AudioMgr.qh);
                        this.playQHSpine();
                        this.equipAttr.star = reve.data.star;
                    } else {
                        this.main.showTips('升级失败');
                    }
                    this.curGoodItem = [];
                    this.mEquipItem.flushEquip(this.main);
                    this.flushEquipPag();
                    this.equip.flushEquip(this.petIcon)
                    this.equip.flushPagSize()
                    this.flushXH();
                    this.equip.mEquipPack.setEquipType(this.equip.curPage);
                } else {
                    this.main.showTips(reve.message);
                }
            } else {
                this.main.showTips('网络异常');
            }
        });
    }

    playQHSpine() {
        this.spine1.setAnimation(0, "atk", false);
        this.spine1.setCompleteListener(() => {
            this.spine1.setAnimation(0, "idle", false);
            this.spine2.node.active = true;
            this.spine2.setAnimation(0, "animation", false);
            this.spine2.setCompleteListener(() => {
                this.spine2.node.active = false;
                this.spine1.clearTrack(0);
            })
        })
    }
}
