import { player } from '../protocols/user/type';
import { HeroShared } from './hero';
import { PublicShared } from './public';


export class PlayerShared {
    /**获取主角buff */
    static getBuff(player: player) {
        let zjBuff: k_v<number> = {
            zj_atk: PublicShared.eval(G.gc.com.zjcz_atk.value as any as string, { lv: player.lv }),
            zj_def: PublicShared.eval(G.gc.com.zjcz_def.value as any as string, { lv: player.lv }),
            zj_liliang: PublicShared.eval(G.gc.com.zjcz_liliang.value as any as string, { lv: player.lv }),
            zj_zhishi: PublicShared.eval(G.gc.com.zjcz_zhishi.value as any as string, { lv: player.lv }),
            zj_mofa: G.gc.com.zj_mofa.value + PublicShared.eval(G.gc.com.zjcz_zhishi.value as any as string, { lv: player.lv })
        };

        if (G.gc.tujian_jc[player.tujianLv]) {
            PublicShared.mergeProperty(zjBuff, G.gc.tujian_jc[player.tujianLv].buff);
        }
        if (player.wxcLv && player.wxcLv.lv >= 1) {
            let wxcbuff = {}
            let wxcData = player.wxcLv;
            let wxcConf = JSON.parse(JSON.stringify(G.gc.weixiuchang_zz))
            // 计算当前等级之前的属性
            for(let i = 1; i < wxcData.lv; i++) {
                let confData = wxcConf[i]
                if(!confData) continue;
                // 计算buff1
                for(let k in confData.need) {
                    let n = confData.need[k].n
                    let buff = confData.buff[k]
                    Object.keys(buff).forEach(key => buff[key] *= n)
                    PublicShared.mergeProperty(wxcbuff, buff)
                }
                // 计算buff2 
                PublicShared.mergeProperty(wxcbuff, confData.buff2)
            }
            // 计算当前等级的属性
            let currentLvBuffArr = wxcConf[wxcData.lv].buff
            for(let i in wxcData.ids) {
                let currentLvBuff = currentLvBuffArr[i]
                Object.keys(currentLvBuff).forEach(key => currentLvBuff[key] *= wxcData.ids[i])
                PublicShared.mergeProperty(wxcbuff, currentLvBuff)
            }
            // 整合入全部属性
            PublicShared.mergeProperty(zjBuff, wxcbuff)
            // let wxcbuff = {};
            // let curWxcLv = player.wxcLv.lv + 1;
            // for (let lv = 1; lv < curWxcLv; lv++) {
            //     for (let key in G.gc.weixiuchang_zz[lv].buff) {
            //         PublicShared.mergeProperty(wxcbuff, G.gc.weixiuchang_zz[lv].buff[key]);
            //     }
            //     G.gc.weixiuchang_zz[lv].buff2 && PublicShared.mergeProperty(wxcbuff, G.gc.weixiuchang_zz[lv].buff2);
            // }
            // // 按装配比例获取属性
            // if (player.wxcLv.ids.length > 0) {
            //     player.wxcLv.ids.forEach(id => {
            //         G.gc.weixiuchang_zz[curWxcLv].buff[id + 1] && PublicShared.mergeProperty(wxcbuff, G.gc.weixiuchang_zz[curWxcLv].buff[id + 1]);;
            //     });
            // }
            // PublicShared.mergeProperty(zjBuff, wxcbuff);
        }

        if (player?.renown) {
            for (let lv = 1; lv <= player.renown; lv++) {
                G.gc.mw_dj[lv].buff && PublicShared.mergeProperty(zjBuff, G.gc.mw_dj[lv].buff);
            }
        }

        if (player?.skills) {
            for (let [id, lv] of Object.entries(player.skills)) {
                if (lv && G.gc.xunlianjihua[id].use == 2) {
                    PublicShared.mergeProperty(zjBuff, Object.fromEntries([G.gc.xunlianjihua[id].skill].map(k => [k, PublicShared.eval(G.gc.xunlianjihua[id].v[0], { slv: lv })])));
                }
            }
        }

        //最后进行加成属性计算
        for (let k in zjBuff) {
            if (k.indexOf('pro') == -1) continue;
            let buffKey = k.replace('pro', '');
            if (zjBuff[buffKey]) {
                zjBuff[buffKey] += zjBuff[buffKey] * zjBuff[k];
                zjBuff[k] = 0;
            }
        }

        // 重新赋值魔法数值，知识有额外加成
        zjBuff.zj_mofa = G.gc.com.zj_mofa.value + zjBuff.zj_zhishi;
        HeroShared.amendAttr(zjBuff);

        return zjBuff;
    }
    static getMingDaoBuff(player: player, key?: string) {
        let obj: k_v<any> = {};

        for (let [id, lv] of Object.entries(player.shoucangping)) {
            if (lv >= G.gc.mingdao[id].level) {
                PublicShared.mergeProperty(obj, G.gc.mingdao[id].privilege);
            }
        }

        if (key) {
            return obj[key];
        }
        return obj;
    }
}