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 FFCalAttr from "../../data/FFCalAttr";
import { __EquipData, __EquipQHData } 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 EquipQHMax extends cc.Component {

    @property(GoodItem)
    mEquipItem: GoodItem = null;

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

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

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

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

    @property(cc.Label)
    mQHLevel1: cc.Label = null;//当前强化等级

    @property(cc.Label)
    mQHLevel2: cc.Label = null;//目标强化等级

    @property(cc.Label)
    mQHLevel: cc.Label = null;//强化等级

    @property(cc.Label)
    mQHBfbLabel: cc.Label = null;//强化百分比

    @property(cc.ProgressBar)
    mQHBfbBar: cc.ProgressBar = null;//强化百分比

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

    @property(cc.Label)
    mQHMaxLabel: cc.Label = null;//上限突破次数

    @property(GoodItem)
    mXHEquipItem: GoodItem = null;

    @property(cc.Node)
    mOneKeyBt: cc.Node = 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: GoodItem = null

    private main: Main = null;

    private MAX_MONEY = [5000, 10000, 15000, 20000, 25000, 0];
    public tpType: number = 0; // 0 突破人物装备  1 突破背包装备
    public equipAttr: EquipAttr = null;
    public equipData: __EquipData = null;
    onLoad() {
    }

    public init(equip: Equip, petIcon: PetIcon, equipAttr: EquipAttr) {
        this.tpType = 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);
        this.flushEquipPag()
    }
    /**
     * 选择当前页卡
     */
    private checkToggle() {
        let scrollView = this.mContent.parent.parent.getComponent(cc.ScrollView)
        scrollView.stopAutoScroll()
        scrollView.scrollToTop()
        this.curGoodItem = null
        this.mXHEquipItem.setNull()
        this.flushToggle()
    }
    private flushToggle() {
        this.flushEquipPag()
        let equipQH = this.node.getComponent(EquipQH)
        equipQH.mQHFull.active = false
        this.mOneKeyBt.active = false
        this.mEquipItem.initEquip(this.main, this.equipAttr)
        this.mEquipName.string = i18n.t(this.equipData.name)
        this.mEquipName1.string = i18n.t(this.equipData.name)

        let qhMax = this.equipAttr.qhMax
        if (qhMax >= this.equipData.qh_max) {
            this.mFullNode.active = true
        } else {
            this.mFullNode.active = false
        }

        this.mQHLevel1.string = (this.equipData.level + qhMax * 5) + ''

        this.mQHMoney.string = '' + this.MAX_MONEY[this.equipAttr.qhMax]

        this.flushLevel2()
        this.flushAttr(this.equipAttr.PI)

        equipQH.setSortCallback((type: number) => {
            if (type == 0) {//品质排序
                EquipQH.sortPag(0, this.mContent)
            } else {//战力排序
                EquipQH.sortPag(1, this.mContent)
            }
        })
    }

    private flushLevel2() {
        let max = this.equipAttr.qhMax
        if (this.curGoodItem) {
            max += 1
        }
        if (max > 5) {
            max = 5
        }
        this.mQHLevel2.string = (this.equipData.level + max * 5) + ''
    }
    /**
      * 刷新装备背包
      */
    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.id == 20001 || element.id == this.equipAttr.id) {
                if (this.equipAttr.__index == element.__index) {
                    this.tpType = 1;
                } else {
                    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);
                    if (element.id == 20001) {
                        node.zIndex = 0
                    } else {
                        node.zIndex = equipItem.equipData.pz
                    }

                    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();
            node.zIndex = cc.macro.MAX_ZINDEX - 1
        }

    }

    /**
     * 刷新属性
     */
    private flushAttr(PI) {
        let equipData = this.equipData
        let __qhList = this.main.sManage.getEquipQHByType(equipData)
        let qhLevel = FFCalAttr.getEquipQHLevel(this.main, PI, equipData)

        let tempAttrExp = 0
        let _qhAttr1: __EquipQHData = null
        let _qhAttr2: __EquipQHData = null
        if (qhLevel > 0) {
            _qhAttr1 = __qhList[qhLevel - 1]
        }
        if (qhLevel >= __qhList.length) {
            _qhAttr2 = __qhList[qhLevel - 1]
        } else {
            _qhAttr2 = __qhList[qhLevel]
        }

        let maxQH = equipData.level + this.equipAttr.qhMax * 5;
        if (qhLevel > maxQH) {
            qhLevel = maxQH
        }

        this.mQHLevel.string = 'Lv.' + qhLevel + '/' + maxQH;

        let curExp = PI - tempAttrExp
        let uplevelExp = _qhAttr2.exp - tempAttrExp

        let bfb = curExp / uplevelExp;
        if (bfb > 1) {
            bfb = 1
        }
        this.mQHBfbBar.progress = bfb
        this.mQHBfbLabel.string = Math.floor(bfb * 100) + '%'

        this.mQHMaxLabel.string = i18n.t('上限突破次数') + this.equipAttr.qhMax + '/5'

    }


    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 (this.curGoodItem) {
            this.curGoodItem.node.opacity = 255
        }
        this.curGoodItem = goodItem
        this.curGoodItem.node.opacity = 100
        this.mXHEquipItem.initEquip(this.main, goodItem.equip)

        this.flushLevel2()
    }
    /**
     * 突破
     */
    public onclickTP() {
        if (this.tpType == 0) {
            this.tpFunc();
        } else if (this.tpType == 1) {
            this.tpFunc1();
        }
    }

    tpFunc() {
        if (!this.curGoodItem) {
            this.main.showTips('没有添加任何材料')
            return
        }
        let msg = {
            petId: this.petIcon.id,
            equipId: this.equipAttr.id,
            equipIndex: this.curGoodItem.equip.__index
        }
        this.main.gameHttp.sendJson('equip/v1/qhMax', msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    this.main.playerEffectByPath(AudioMgr.qh);
                    this.playQHSpine();
                    this.equipAttr.qhMax += 1
                    this.curGoodItem = null
                    this.mXHEquipItem.setNull()
                    this.flushToggle()
                    this.equip.flushEquip(this.petIcon)
                    this.equip.flushPagSize()
                    this.main.showTips("强化上限成功");
                } else {
                    this.main.showTips(reve.message);
                }
            } else {
                this.main.showTips('网络异常');
            }
        });
    }

    tpFunc1() {
        if (!this.curGoodItem) {
            this.main.showTips('没有添加任何材料')
            return
        }
        let msg = {
            equipIndex1: this.equipAttr.__index,
            equipIndex2: this.curGoodItem.equip.__index
        }
        this.main.gameHttp.sendJson('equip/v1/qhMax1', msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    this.main.playerEffectByPath(AudioMgr.qh);
                    this.playQHSpine();
                    this.equipAttr.qhMax += 1
                    this.curGoodItem = null
                    this.mXHEquipItem.setNull()
                    this.flushToggle()
                    this.equip.mEquipPack.setEquipType(this.equip.curPage);

                    this.equip.flushEquip(this.petIcon)
                    this.equip.flushPagSize()
                } 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);
            })
        })
    }
}
