import Main from "../../main/Main";
import CUtil from "../../util/CUtil";
import FFCalAttr from "./FFCalAttr";
import RedNode from "./RedNode";
import { __EquipData } from "./sdata/SManage";
import { EquipAttr, PetAttr } from "./udata/Player";

/**
 * 计算红点
 */
export default class RedPoint {
    /**
     * 角色查看界面是否有红点
     * @param main 
     */
    public static petsRedPoint(main: Main):boolean{
        let petMap = main.player.pet
        for (const id in petMap) {
            let pet = petMap[id];
            if(RedPoint.petRedPoint(main,pet)){
                return true
            }
        }
        return false
    }

    /**
     * 伙伴是否有可使用的装备
     * @param main 
     * @param petAttr 
     * @returns 
     */
    public static petRedPoint(main: Main, petAttr: PetAttr): boolean {
        let equip = main.player.equip
        for (let i = 0; i < equip.length; i++) {
            const element = equip[i];
            if (RedPoint.equipRedPoint(main, element, petAttr)) {
                return true
            }
        }
        return false
    }
    /**
     * 装备是否有红点
     * @param element 
     * @param petAttr 
     * @returns 
     */
    public static equipRedPoint(main: Main, equipAttr: EquipAttr, petAttr: PetAttr): boolean {
        //1.判断职业是否符合
        let _equipA: __EquipData = main.sManage.getEquipById(equipAttr.id);
        if(_equipA.type > 5){//不是装备
            return false
        }
        if(_equipA.occ != 0){//有职业限制
            if (_equipA.occ != petAttr.id) {
                return false
            }
        }
       
        let oldEquipAttr = RedPoint.getPetAttr(main, _equipA.type, petAttr)
        if (oldEquipAttr == null) {//如果这个位置没有装备
            return true
        }
        let _equipB: __EquipData = main.sManage.getEquipById(oldEquipAttr.id);
        let ffData1 = FFCalAttr.getEquipAttr(main, equipAttr, _equipA)
        let ffData2 = FFCalAttr.getEquipAttr(main, oldEquipAttr, _equipB)
        return ffData1.zdl > ffData2.zdl
    }
    /**
     * 
     * @param equipAttr 
     * @param petAttr 
     * @returns 伙伴穿的相同类型的装备
     */
    public static getPetAttr(main: Main, type: number, petAttr: PetAttr): EquipAttr {
        let petEquips = petAttr.equips

        for (let i = 0; i < petEquips.length; i++) {
            const element = petEquips[i];
            let _equipB: __EquipData = main.sManage.getEquipById(element.id);
            if (type == _equipB.type) {
                return element
            }
        }

        return null;
    }



    /**
    * 
    * @param pNode 
    * @param force 是否强制添加红点
    */
    public static addRed(main:Main,pNode: cc.Node, force?: boolean,lr?:boolean) {
        let node = pNode.getChildByName('redpoint');
        if(node){
            if(!node.active){
                if(force){
                    node.active = true;
                }else{
                    let nowTime = CUtil.getNowTime();
                    let redNode = node.getComponent(RedNode);
                    if(nowTime - redNode.time > 600){
                        node.active = true;
                    }
                }
            }
            return;
        }
        node = new cc.Node('redpoint');
        let sprite = node.addComponent(cc.Sprite);
        sprite.spriteFrame = main.RED_POINT;
        node.addComponent(RedNode);
        node.parent = pNode;
        let rect = main.RED_POINT.getRect();
        if(lr){
            node.x = -(pNode.width - rect.width) / 2;
            node.y = (pNode.height - rect.height) / 2;
        }else{
            node.x = (pNode.width - rect.width) / 2;
            node.y = (pNode.height - rect.height) / 2;
        }
       
    }
    public static removeRed(pNode: cc.Node) {
        let node = pNode.getChildByName('redpoint');
        if (node) {
            node.active = false;
            let redNode = node.getComponent(RedNode);
            redNode.time = CUtil.getNowTime();
        }
    }

}
