import Main from "../../main/Main";
import { __EquipData,__GodCardLevel,__GodReborn } from "./sdata/SManage";
import { EquipAttr, GodCardAttr, PetAttr } from "./udata/Player";

//属性
export interface FFAttr{
    id:number,

    post:number,//职业
    type:number,//1:小怪，2:boss,3:伙伴

    path:string,
    atk:number,
    def:number,
    hp:number,
    sp:number,
    // bjgl:number,//暴击概率
    // js:number,//减伤
    // shjc:number,//伤害加成
    // jnsh:number,//技能伤害

    water:number
    fire:number
    wind:number
    thunder:number
    earth:number

    sd:number,//射程

    weaponSkill//武器技能
    suitId:number//套装属性
    suitCount:number//套装凑齐数量
    attrs:Array<any>//装备携带的副属性{K:id,V:value}
    

    zdl:number,
    skin:string,//皮肤
    weapon:string,//武器
    bullet:string,//使用的子弹
}
/**
 * 属性计算
 */

export default class FFCalAttr {

    public static getAttr(main:Main,petAttr:PetAttr):FFAttr{
        let roleData = main.sManage.getRoleById(petAttr.id);
        let data = {
            id:petAttr.id,
            type:3,
            post:roleData.post,
            path:'prefab/role/'+roleData.act,
            atk:roleData.atk,
            def:roleData.def,
            hp:roleData.hp,
            sp:roleData.sp,

            water:0,
            fire:0,
            wind:0,
            thunder:0,
            earth:0,

            sd:0,
            weaponSkill:0,
            suitId:0,
            suitCount:0,
            attrs:[],
            zdl:0,
            skin:'30000',//皮肤
            weapon:'',//武器
            bullet:null,
        };
        for (let i = 0; i < petAttr.equips.length; i++) {
            const element = petAttr.equips[i];
            //基础属性
            let equipData = main.sManage.getEquipById(element.id);
            if(equipData.type == 1){//武器
                data.weapon = ''+equipData.id
                data.bullet = equipData.bulletEffect
            }else  if(equipData.type == 5){//皮肤
                data.skin = ''+equipData.id
            }
            data.atk += equipData.atk;
            data.def += equipData.def;
            data.hp += equipData.hp;
            data.sp += equipData.sp;
            data.sd += equipData.sd;
            if(equipData.skill > 0){
                data.weaponSkill = equipData.skill
            }
            //计算附加属性
            if(element.attr){
                for (let i = 0; i < element.attr.length; i++) {
                    const attr = element.attr[i];
                    let addAttr = true;
                    for (let j = 0; j < data.attrs.length; j++) {
                        const tmp = data.attrs[j];
                        if(tmp.K == attr.K){
                            tmp.V += attr.V
                            addAttr = false
                            break
                        }
                    }
                    if(addAttr){
                        data.attrs.push({
                            K:attr.K,
                            V:attr.V
                        })
                    }
                }
            }
            
            //计算强化属性
            let qhLevel = FFCalAttr.getEquipQH(main,element,equipData)
            if(qhLevel > 0){
                let __qhList = main.sManage.getEquipQHByType(equipData)
                let _qhAttr1 = __qhList[qhLevel - 1]
                data.atk += _qhAttr1.atk
                data.def += _qhAttr1.def
                data.hp += _qhAttr1.hp
                data.sp += _qhAttr1.sp
            }
            if(element.star > 0){
                data.atk += Math.floor(data.atk*element.star/10)
                data.def += Math.floor(data.def*element.star/10)
                data.hp += Math.floor(data.hp*element.star/10)
                data.sp += Math.floor(data.sp*element.star/10)
            }
           
        }
        //计算附加属性
        for (let i = 0; i < data.attrs.length; i++) {
            const element = data.attrs[i];
            if(element.K == 1){//攻
                data.atk += Math.floor(data.atk*element.V/100)
            }else if(element.K == 2){//防
                data.def += Math.floor(data.def*element.V/100)
            }else if(element.K == 3){//血
                data.hp += Math.floor(data.hp*element.V/100)
            }else if(element.K == 4){//速
                data.sp += Math.floor(data.sp*element.V/100)
            }else if(element.K == 5){//攻
                data.atk += Math.floor(data.atk*element.V/100)
            }else if(element.K == 6){//防
                data.def += Math.floor(data.def*element.V/100)
            }else if(element.K == 7){//血
                data.hp += Math.floor(data.hp*element.V/100)
            }else if(element.K == 8){//速
                data.sp += Math.floor(data.sp*element.V/100)
            }
        }
        //神卡增加属性
        let godCard = main.player.godCard
        for (const id in godCard) {
            let godCardAttr:GodCardAttr = godCard[id]
            let __godCard = main.sManage.getGodCardById(godCardAttr.id)
            FFCalAttr.addCardAttr(data,__godCard)

            if(godCardAttr.level > 0){
                let tmp = __godCard.list[godCardAttr.level-1]
                FFCalAttr.addCardAttr(data,tmp)
            }
            if(godCardAttr.reborn > 0){
                FFCalAttr.addCardAttr(data,__godCard.godReborn)
            }
        }
        //头像增加属性
        let roleIcons = main.player.roleIcon.open
        for (let i = 0; i < roleIcons.length; i++) {
            const element = roleIcons[i];
            let temp = main.sManage.getRoleIconById(element)
            data.atk += temp.atk
            data.def += temp.def
            data.hp += temp.hp
            data.sp += temp.sp
        }

        data.zdl = FFCalAttr.getZdl(data)
        if(!data.bullet){
            if(roleData.post == 1){
                data.bullet = main.sManage.getEquipById(1001).bulletEffect
            }else if(roleData.post == 2){
                data.bullet = main.sManage.getEquipById(1102).bulletEffect
            }else if(roleData.post == 3){
                data.bullet = main.sManage.getEquipById(1202).bulletEffect
            }
        }
        return data;
    }

    private static addCardAttr(data:FFAttr,attr){
        data.atk += attr.atk
        data.def += attr.def
        data.hp += attr.hp
        data.sp += attr.sp

        data.atk += attr.water
        data.def += attr.fire
        data.hp += attr.wind
        data.sp += attr.thunder
        data.sp += attr.earth
    }

    public static getEquipAttr(main:Main,equip:EquipAttr,equipData:__EquipData):FFAttr{
        let data = {
            id:0,
            type:3,
            post:0,
            path:null,
            atk:equipData.atk,
            def:equipData.def,
            hp:equipData.hp,
            sp:equipData.sp,

            water:0,
            fire:0,
            wind:0,
            thunder:0,
            earth:0,

            sd:0,
            weaponSkill:0,
            suitId:0,
            suitCount:0,
            attrs:[],
            zdl:0,
            skin:'30000',//皮肤
            weapon:'',//武器
            bullet:null
        };
        let qhLevel = FFCalAttr.getEquipQH(main,equip,equipData)
        if(qhLevel > 0){
            let __qhList = main.sManage.getEquipQHByType(equipData)
            let _qhAttr1 = __qhList[qhLevel - 1]
            data.atk += _qhAttr1.atk
            data.def += _qhAttr1.def
            data.hp += _qhAttr1.hp
            data.sp += _qhAttr1.sp
        }
        if(equip.star > 0){
            data.atk += Math.floor(data.atk*equip.star/10)
            data.def += Math.floor(data.def*equip.star/10)
            data.hp += Math.floor(data.hp*equip.star/10)
            data.sp += Math.floor(data.sp*equip.star/10)
        }
         //计算附加属性
        //  if(equip.attr){
        //     for (let i = 0; i < equip.attr.length; i++) {
        //         const element = equip.attr[i];
        //         if(element.K == 1){//攻
        //             data.atk += Math.floor(data.atk*element.V/100)
        //         }else if(element.K == 2){//防
        //             data.def += Math.floor(data.def*element.V/100)
        //         }else if(element.K == 3){//血
        //             data.hp += Math.floor(data.hp*element.V/100)
        //         }else if(element.K == 4){//速
        //             data.sp += Math.floor(data.sp*element.V/100)
        //         }else if(element.K == 5){//攻
        //             data.atk += Math.floor(data.atk*element.V/100)
        //         }else if(element.K == 6){//防
        //             data.def += Math.floor(data.def*element.V/100)
        //         }else if(element.K == 7){//血
        //             data.hp += Math.floor(data.hp*element.V/100)
        //         }else if(element.K == 8){//速
        //             data.sp += Math.floor(data.sp*element.V/100)
        //         }
        //     }
        // }

        data.zdl = FFCalAttr.getZdl(data)
        return data
    }
    
    public static getZdl(data):number{
        return data.atk + data.def + Math.floor(data.hp/10) + data.sp
    }
    /**
     * 计算装备强化等级
     */
    public static getEquipQH(main:Main,equip:EquipAttr,equipData:__EquipData):number{
        if(equipData.type > 4){
            return 0
        }
        let qhList = main.sManage.getEquipQHByType(equipData)
        let qhLevel = -1
        for (let i = 0; i < qhList.length; i++) {
            const element = qhList[i];
            if(element.exp > equip.PI){
                qhLevel = i
                break
            }
        }
        if(qhLevel == -1){
            qhLevel = qhList.length
        }
        let max = equipData.level + equip.qhMax*5

        if(qhLevel > max){
            qhLevel = max
        }
        
        return qhLevel
    }
    /**
     * 计算装备强化等级
     */
    public static getEquipQHLevel(main:Main,PI:number,equipData:__EquipData):number{
        if(equipData.type > 4){
            return 0
        }
        let qhList = main.sManage.getEquipQHByType(equipData)
        for (let i = 0; i < qhList.length; i++) {
            const element = qhList[i];
            if(element.exp > PI){
                return i
            }
        }
        return qhList.length
    }

    /**
     * 获取神卡属性
     * @param godCardAttr 
     */
    public static getGodCardAttr(main:Main,godCardAttr:GodCardAttr):FFAttr{
        let _godCard = main.sManage.getGodCardById(godCardAttr.id)
        
        let data = {
            id:0,
            type:3,
            post:0,
            path:null,
            atk:_godCard.atk,
            def:_godCard.def,
            hp:_godCard.hp,
            sp:_godCard.sp,
            bjgl:0,

            water:_godCard.water,
            fire:_godCard.fire,
            wind:_godCard.wind,
            thunder:_godCard.thunder,
            earth:_godCard.earth,

            sd:0,
            weaponSkill:0,
            suitId:0,
            suitCount:0,
            attrs:[],
            zdl:0,
            skin:'30000',//皮肤
            weapon:'',//武器
            bullet:null
        };
        if(godCardAttr.level > 0){
            let _godCardLevel = _godCard.list[godCardAttr.level - 1]
            data.atk += _godCardLevel.atk
            data.def += _godCardLevel.def
            data.hp += _godCardLevel.hp
            data.sp += _godCardLevel.sp
    
            data.water += _godCardLevel.water
            data.fire += _godCardLevel.fire
            data.wind += _godCardLevel.wind
            data.thunder += _godCardLevel.thunder
            data.earth += _godCardLevel.earth
        }

        if(godCardAttr.reborn > 0){
            let rebornData:__GodReborn = _godCard.godReborn
            
            data.atk += rebornData.atk
            data.def += rebornData.def
            data.hp += rebornData.hp
            data.sp += rebornData.sp

            data.water += rebornData.water
            data.fire += rebornData.fire
            data.wind += rebornData.wind
            data.thunder += rebornData.thunder
            data.earth += rebornData.earth
        }


        return data
    }



}
