import { DividendModel } from './../../../db/Dividend';
import { Application, BackendSession, ChannelService } from "pinus";
import { AUCTION_STAGE, DEBUG_MAGIC_WORD, STATUS, OFFER_RATIO, CURRENCY_BY_TYPE, CURRENCY_TYPE, DATA_NAME, LOT_STATUS } from "../../../consts";
import { LotModel } from "../../../db/Lot";
import { ItemReward } from "../../../domain/dbGeneral";
import { resResult } from "../../../pubUtils/util";
import { auctionStage, calculateDividend, genAuction, sendUngotDividend, startGuildAuction, startWorldAuction, stopAuction, todayGuildBegin, getBasePrice, debugAuctionLots, officialAuctionLots, auctionBidStatus, getMaxPrice, guildBidStatus, getAuction } from "../../../services/auctionService";
import { addItems, handleCost } from '../../../services/rewardService';
import { getSimpleRoleInfo } from '../../../services/roleService';
import { getRoleOnlineInfo } from '../../../services/redisService';
import { lockData } from '../../../services/redLockService';

export default function (app: Application) {
    return new AuctionHandler(app);
}

export class AuctionHandler {
    channelService: ChannelService;
    constructor(private app: Application) {
        this.channelService = app.get('channelService');
    }

    async getAuction(msg: { magicWord: string }, session: BackendSession) {
        const { magicWord } = msg;

        const guildCode = session.get('guildCode');
        if (!guildCode) return resResult(STATUS.GUILD_NOT_FOUND);
        let result = await getAuction(guildCode, session, magicWord);

        return resResult(STATUS.SUCCESS, result);
    }

    async offer(msg: { code: string, max: boolean }, session: BackendSession) {
        const { code, max } = msg;
        const roleId = session.get('roleId');
        const roleName = session.get('roleName');
        const sid = session.get('sid');
        const serverId = session.get('serverId');
        const guildCode = session.get('guildCode');
        let res: any = await lockData(serverId, DATA_NAME.AUCTION_LOT, code);// 加锁

        try {
            if (!res) {
                return resResult(STATUS.REDLOCK_ERR);
            }

            const lot = await LotModel.findLot(code);
            if (!lot || lot.status === LOT_STATUS.SOLD || lot.status === LOT_STATUS.MAX) {
                res.releaseCallback();
                return resResult(STATUS.GUILD_LOT_NOT_FOUND);
            }

            if (lot.auctionStage === AUCTION_STAGE.GUILD && lot.guildCode !== guildCode) {
                res.releaseCallback();
                return resResult(STATUS.AUCTION_GUILD_MEMBER_ONLY);
            }

            const { curBuyer, curPrice, maxPrice, gid, count, bidRoles, watchingRoles } = lot;
            if (curBuyer === roleId && !max) {
                res.releaseCallback();
                return resResult(STATUS.LOT_OFFER_SERIAL);
            }
            let newPrice = Math.floor(curPrice * OFFER_RATIO);
            let maxFlag = max;
            if (newPrice >= maxPrice) {
                newPrice = maxPrice;
                maxFlag = true;
            }
            const costRes = await handleCost(roleId, sid, [{ id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD), count: newPrice }]);
            if (!costRes) {
                res.releaseCallback();
                return resResult(STATUS.ROLE_COIN_NOT_ENOUGH);
            }

            if (curBuyer) {
                const { roleName: buyerName } = await getSimpleRoleInfo(curBuyer);
                const { isOnline, sid: buyerSid } = await getRoleOnlineInfo(curBuyer);
                await addItems(curBuyer, buyerName, buyerSid, [{ id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD), count: curPrice }]);
                if(isOnline) {
                    this.channelService.pushMessageByUids('onAuctionOver', resResult(STATUS.SUCCESS, { code }), [{ uid: curBuyer, sid: buyerSid }]);
                }
            }

            if (maxFlag) {
                newPrice = maxPrice;
                await addItems(roleId, roleName, sid, [{ id: gid, count }]);
            }
            bidRoles.push({ roleId, price: newPrice, time: new Date() });
            const newLot = await LotModel.updateLot({ code, curBuyer: roleId, curPrice: newPrice, bidRoles, status: max ? LOT_STATUS.MAX : (maxFlag ? LOT_STATUS.SOLD : LOT_STATUS.ING), watchingRoles: Array.from(new Set([...watchingRoles, roleId])) });
            res.releaseCallback();

            const incPrice = newPrice - (curBuyer ? curPrice : 0);
            let newDividend = null;
            if (lot.auctionStage === AUCTION_STAGE.GUILD) {
                const dividend = await DividendModel.updateLot(code, gid, newPrice, incPrice, max);
                newDividend = await calculateDividend(dividend);
            }
            return resResult(STATUS.SUCCESS, { lot: newLot, dividend: newDividend });
        } catch (e) {
            console.log('offer got err:', e);
            res.releaseCallback();
            return resResult(STATUS.INTERNAL_ERR);
        }
    }

    async watchLot(msg: { code: string }, session: BackendSession) {
        const roleId = session.get('roleId');
        const { code } = msg;
        const lot = await LotModel.watchLot(code, roleId);
        if (!lot) return resResult(STATUS.WRONG_PARMS);
        return resResult(STATUS.SUCCESS, { lot });
    }

    async unWatchLot(msg: { code: string }, session: BackendSession) {
        const roleId = session.get('roleId');
        const { code } = msg;
        const lot = await LotModel.unWatchLot(code, roleId);
        if (!lot) return resResult(STATUS.WRONG_PARMS);
        return resResult(STATUS.SUCCESS, { lot });
    }

    async checkDividend(msg: {}, session: BackendSession) {
        const begin = todayGuildBegin();
        const guildCode = session.get('guildCode');
        if (!guildCode) return resResult(STATUS.GUILD_NOT_FOUND);
        const dividends = await DividendModel.findGuildDividendsByBegin(guildCode, begin);
        return resResult(STATUS.SUCCESS, { dividends });
    }

    async getDividend(msg: { code: string }, session: BackendSession) {
        const roleId = session.get('roleId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');
        const { code } = msg;
        const dividendData = await DividendModel.findReadyDividend(code);
        if (!dividendData) return resResult(STATUS.DIVIDEND_NOT_READY);
        const { dividends } = dividendData;
        const dividend = dividends.find(item => { return item.roleId === roleId });
        if (!dividend) return resResult(STATUS.DIVIDEND_GUILD_PLAYER_ONLY);
        await addItems(roleId, roleName, sid, [{ id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD), count: dividend.total }]);
        await DividendModel.updateReceiveStatus(dividendData.code, roleId);
        return resResult(STATUS.SUCCESS, { dividend });
    }

    async myWatching(msg: {}, session: BackendSession) {
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const begin = todayGuildBegin();
        const lots = await LotModel.watchingLotsByBegin(serverId, roleId, begin);
        const stage = auctionStage();
        return resResult(STATUS.SUCCESS, { lots: stage === AUCTION_STAGE.END ? [] : lots });
    }

    async offerRecs(msg: { count: number }, session: BackendSession) {
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const lotsData = await LotModel.recentBidLots(serverId, roleId, msg.count);
        const bidRecs = lotsData.map(lot => {
            const bidRec = lot.bidRoles.find(role => { return role.roleId === roleId });
            const { gid } = lot;
            return { ...bidRec, gid, status: auctionBidStatus(roleId, lot) };
        });
        return resResult(STATUS.SUCCESS, { bidRecs });
    }

    async guildLotRecs(msg: { count: number }, session: BackendSession) {
        const guildCode = session.get('guildCode');
        const dividends = await DividendModel.findDividendsByGuild(guildCode, msg.count);
        let lotsData = [];
        dividends.forEach(dividend => {
            const lots = dividend.lots.map(lot => {
                return { ...lot, sourceType: dividend.sourceType, dividendStatus: dividend.status };
            })
            lotsData = [...lotsData, ...lots];
        });
        const lotRecs = lotsData.map(lot => {
            const { gid, count, price: lotPrice } = lot;
            const price = lotPrice === 0 ? getBasePrice(gid, count) : lotPrice;
            const sold = guildBidStatus(lot);
            return { ...lot, price, sold };
        });

        return resResult(STATUS.SUCCESS, { lotRecs });
    }

    // ! 测试接口
    async debugSetDividendStatus(msg: { magicWord: string, sourceType: number, status: number }, session: BackendSession) {
        const { magicWord, sourceType, status } = msg;
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }

        const guildCode: string = session.get('guildCode');
        if (!guildCode) return resResult(STATUS.GUILD_NOT_FOUND)
        const dividend = await DividendModel.updateDividendStatus(guildCode, sourceType, status);
        return resResult(STATUS.SUCCESS, { dividend });
    }

    // ! 测试接口
    async debugSetLotStage(msg: { magicWord: string, code: string, auctionStage: number }, session: BackendSession) {
        const { magicWord, code, auctionStage } = msg;
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }
        const lot = await LotModel.updateLotStage(code, auctionStage);
        return resResult(STATUS.SUCCESS, { lot });
    }

    // ! 测试接口
    async debugAddLots(msg: { magicWord: string, sourceType: number, sourceCode: string, rewards: ItemReward[] }, session: BackendSession) {
        const { magicWord, sourceType, sourceCode, rewards } = msg;
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }

        const guildCode: string = session.get('guildCode');
        const serverId: number = session.get('serverId');
        if (!guildCode) return resResult(STATUS.GUILD_NOT_FOUND)

        const result = await genAuction(guildCode, sourceType, sourceCode, serverId, rewards);
        if (!result) {
            return resResult(STATUS.WRONG_PARMS);
        }

        return resResult(STATUS.SUCCESS, result);
    }

    // ! 测试接口
    async debugScheduleStartGuild(msg: { magicWord: string }, session: BackendSession) {
        const { magicWord } = msg;
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }
        const result = await startGuildAuction();
        if (result === true) {
            return resResult(STATUS.SUCCESS);
        }
        return resResult(STATUS.INTERNAL_ERR);
    }

    // ! 测试接口
    async debugScheduleStartWorld(msg: { magicWord: string }, session: BackendSession) {
        const { magicWord } = msg;
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }
        const result = await startWorldAuction();
        if (result === true) {
            return resResult(STATUS.SUCCESS);
        }
        return resResult(STATUS.INTERNAL_ERR);
    }

    // ! 测试接口
    async debugScheduleStopAuction(msg: { magicWord: string }, session: BackendSession) {
        const { magicWord } = msg;
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }
        const result = await stopAuction();
        if (result === true) {
            return resResult(STATUS.SUCCESS);
        }
        return resResult(STATUS.INTERNAL_ERR);
    }

    // ! 测试接口
    async debugScheduleSendUngotDividend(msg: { magicWord: string }, session: BackendSession) {
        const { magicWord } = msg;
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }
        const result = await sendUngotDividend(true);
        if (result === true) {
            return resResult(STATUS.SUCCESS);
        }
        return resResult(STATUS.INTERNAL_ERR);
    }
}
