import Main from "../../../main/Main";
import GoodItem from "../../common/GoodItem";
import PetIcon from "../../common/PetIcon";
import FFCalAttr from "../../data/FFCalAttr";
import RedPoint from "../../data/RedPoint";
import { __EquipData } from "../../data/sdata/SManage";
import { EquipAttr } from "../../data/udata/Player";
import Equip from "./Equip";

/**
 * 装备背包
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class EquipPack extends cc.Component {

    @property(cc.Node)
    mPagContent: cc.Node = null;

    @property(cc.ScrollView)
    mPagScrollView: cc.ScrollView = null;

    @property(cc.Prefab)
    mEquipItem: cc.Prefab = null;

    @property([cc.Node])
    mButtonItem: Array<cc.Node> = [];

    @property(cc.SpriteFrame)
    mCheckIcon: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    mNoCheckIcon: cc.SpriteFrame = null;

    @property
    mCheckX = -282

    @property
    mNoCheckX = -267

    public main:Main;
    public petIcon:PetIcon;
    /**
     * 装备类型
     */
    public equipType:number = 1;

    public equip:Equip;
    onLoad(){
        this.equip = this.node.getComponent(Equip)
        this.main = this.equip.main;
    }
    /**
     * @param pi 穿装备的伙伴
     * @param equipItem 选择的装备
     */
    public init(pi:PetIcon){
        this.petIcon = pi;
        for (let i = 0; i < this.mButtonItem.length; i++) {
            const element = this.mButtonItem[i];
            RedPoint.removeRed(element)
        }
        this.setEquipType(this.equipType)

        this.flushRedPoint()
    }
    //根据每个类型刷新按钮红点
    public flushRedPoint(){
        let equip = this.main.player.equip
        for (let i = 0; i < equip.length; i++) {
            const element = equip[i];
            if (RedPoint.equipRedPoint(this.main, element, this.petIcon.data)) {
                let _equipA: __EquipData = this.main.sManage.getEquipById(element.id);
                if(_equipA.type < this.mButtonItem.length ){
                    let btNode = this.mButtonItem[_equipA.type-1]
                    RedPoint.addRed(this.main,btNode,true,true)
                }
            }
        }
    }
    

    public setEquipType(equipType:number){
        this.mPagScrollView.stopAutoScroll()
        this.mPagScrollView.scrollToTop()
        this.equipType = equipType;
        this.equip.curPage = equipType;
        this.flushPag()
        for (let i = 0; i < this.mButtonItem.length; i++) {
            const element = this.mButtonItem[i];
            let sprite = element.getComponent(cc.Sprite)
            if(i == this.equipType - 1){
                sprite.spriteFrame = this.mCheckIcon
                element.x = this.mCheckX
            }else{
                sprite.spriteFrame = this.mNoCheckIcon
                element.x = this.mNoCheckX
            }
        }
    }

    /**
     * 刷新背包
     */
    public flushPag(){
        if(this.mPagContent.children.length <= 0){
            for (let i = 0; i < 30; i++) {
                let node = cc.instantiate(this.mEquipItem);
                node.parent = this.mPagContent;
            }
        }
        this.flushEquip();
    }
    /**
     * 刷新装备
     */
    public flushEquip(){
        //筛选职业和类型符合的装备
        let pet = this.main.sManage.getRoleById(this.petIcon.id);

        let equips = this.main.player.equip;
        let index = 0;
        let content = this.mPagContent;
        for (let i = 0; i < equips.length; i++) {
            const element = equips[i];
            let _equip = this.main.sManage.getEquipById(element.id);
            if(_equip.type == this.equipType 
                && (_equip.occ == 0 || pet.post == _equip.occ)){
                    if(index >= content.children.length){
                        this.addItem(content);
                    }
                    let node = content.children[index];
                    RedPoint.removeRed(node)
                    let equipItem = node.getComponent(GoodItem);
                    equipItem.initEquip(this.main,element);
                    equipItem.setCallback((gi)=>{
                        this.checkEquip(gi);
                    });
                    let equipAttr = FFCalAttr.getEquipAttr(this.main,equipItem.equip,equipItem.equipData)
                    let zIndex = cc.macro.MAX_ZINDEX - equipAttr.zdl
                    if(zIndex < cc.macro.MIN_ZINDEX){
                        zIndex = cc.macro.MIN_ZINDEX
                    }
                    node.zIndex = zIndex
                    index ++;
            }
        }
        
        for (let i = index; i < content.children.length; i++){
            let node = content.children[i];
            RedPoint.removeRed(node)
            let equipItem = node.getComponent(GoodItem);
            equipItem.setNull();
            node.zIndex = cc.macro.MAX_ZINDEX - 1
        }
    }
    private addItem(content:cc.Node){
        for (let i = 0; i < 5; i++) {
            let node = cc.instantiate(this.mEquipItem);
            node.parent = content;
        }
    }
    public checkEquip(equipItem:GoodItem){
       this.equip.openChangeEquip(equipItem);
    }
    /**
     * 点击切换装备栏
     */
    public onclickType(event,customEventData){
        let equipType = parseInt(customEventData)
        this.setEquipType(equipType)
    }
}
