import {ApiCall} from "tsrpc";
import {PlayerFun} from '../../public/player';
import {UserFun} from '../../public/user';
import {ReqUse, ResUse} from "../../shared/protocols/item/PtlUse";
import {PublicShared} from '../../shared/public/public';
import {TanXianShared} from '../../shared/public/tanxian';
import {event_dldh_addPrize} from '../event/diaoluoduihuan/ApiOpen';
import {getEventPrize} from '../tanxian/ApiEvent';
import {getItemByItemId} from "../../public/item";

const iType = {
    3: 'hero',
    4: 'equip',
    5: 'item'
};

export default async function (call: ApiCall<ReqUse, ResUse>) {

    //ȥ��item Redis���
    let itemInfo = await getItemByItemId(call.uid, call.req.itemId); //call.conn.item[call.req.itemId];
    if (!itemInfo[call.req.itemId]) return call.error(globalThis.lng.item_1);
    let item = itemInfo[call.req.itemId];

    let useNum = call.req.useNum;
    let itemConf = G.gc.item[item?.itemId];

    if (!item) return call.error(globalThis.lng.item_1);
    if (itemConf.type == 1) return call.error(globalThis.lng.item_2);
    if (useNum < 1) return call.error(globalThis.lng.item_3);

    let cutPrize: atn[] = [];
    let sendPrize: atn[] = [];

    if (itemConf.type == 2) {

        if (useNum > item.num) return call.error(globalThis.lng.item_3);
        if (itemConf.useNeed?.length > 0) {
            await PlayerFun.checkNeedIsMeet(call, itemConf.useNeed.map(need => {
                return {...need, n: need.n * useNum};
            }));
            await PlayerFun.cutNeed(call, itemConf.useNeed.map(need => {
                return {...need, n: need.n * useNum};
            }));
        }
        if (itemConf.selecPrize?.length > 0) {
            if (call.req.selectIndex == undefined || !itemConf.selecPrize[call.req.selectIndex]) return call.errorCode(-3);
            sendPrize.push({
                ...itemConf.selecPrize[call.req.selectIndex],
                n: itemConf.selecPrize[call.req.selectIndex].n * useNum
            });
        }

        if (itemConf.diaoluo) {
            sendPrize.push(...PublicShared.randomDropGroup(itemConf.diaoluo, useNum));
        } else if (itemConf.usePrize?.length > 0) {
            itemConf.usePrize.forEach(id => {
                sendPrize.push(...PublicShared.randomDropGroup(id, useNum));
            });
        }

        if (G.gc.other[`item${itemConf.id}`] && itemConf.id == 614) {
            let lv = call.conn.gud.wxcLv.lv;
            let conf = G.gc.other[`item${itemConf.id}`] as _gcType['other']['item614'];
            let find = conf.find(v => lv >= v.lv[0] && lv <= v.lv[1]);
            sendPrize = PublicShared.randomDropGroup(find.dlz).map(dp => {
                return {
                    ...dp,
                    n: dp.n * useNum
                };
            });
        }

        if (itemConf.id == 24) {

            let prize = TanXianShared.getGuaJiPrize(G.gc.tanxian_com.fastGuaJiTime, call.conn.gud.mapId, false);
            let eventPrize = getEventPrize(call.conn.gud.lv, G.gc.tanxian_com.fastGuaJiTime, call.conn.gud.mapId, call.conn.gud.wxcLv?.lv);
            let _p = [].concat(prize.prize, prize.dlz, eventPrize.prize);
            // await event_dldh_addPrize(_p, call, G.gc.tanxian_com.fastGuaJiTime);
            sendPrize = _p.map(atn => ({...atn, n: atn.n * useNum}));
        }

        cutPrize = [{
            a: 'item',
            t: item.itemId,
            n: useNum
        }];

        sendPrize.forEach(atn => {
            if (atn.a == 'shiwu' && !atn.colour) atn.colour = itemConf.colour;
        });
    } else if ([2, 3, 4].includes(itemConf.type)) {
        if (useNum * itemConf.num > item.num) return call.error(globalThis.lng.item_3);
        if (!itemConf[iType[itemConf.type]]) return call.error(globalThis.lng.item_4);
        cutPrize = [
            {
                a: 'item',
                t: item.itemId,
                n: useNum * itemConf.num
            }
        ];
        sendPrize = [
            {
                a: iType[itemConf.type],
                t: itemConf[iType[itemConf.type]].toString(),
                n: useNum
            }
        ];
    } else if ([6, 7, 8].includes(itemConf.type)) {
        if (useNum > item.num) return call.error(globalThis.lng.item_3);
        if (itemConf.type == 6) UserFun.activeModel(call.uid, itemConf.diaoluo.toString(), useNum);
        if (itemConf.type == 7) UserFun.activeHeadFrame(call.uid, itemConf.diaoluo.toString(), useNum);
        if (itemConf.type == 8) UserFun.activeChatFrame(call.uid, itemConf.diaoluo.toString(), useNum);

        cutPrize = [
            {
                a: 'item',
                t: item.itemId,
                n: useNum
            }
        ];
    }

    if (cutPrize && sendPrize) {
        await PlayerFun.cutNeed(call, cutPrize);
        await PlayerFun.sendPrize(call, sendPrize);

        call.succ({
            prize: sendPrize
        });
    } else {
        call.error(globalThis.lng.item_5);
    }
}