import { UserOrderModelType } from '../db/UserOrder';
import { ACTIVITY_TYPE, STATUS } from '../consts';
import { dicRMB } from '../pubUtils/dictionary/DicRMB';
import { makeYuanbaoShopReward } from './activity/yuanbaoService';
import { RoleModel } from '../db/Role';
import { makeMonthlyTicketReward } from './activity/monthlyTicketService';
import { makeSignInVIP } from './activity/signInService';
import { makeDailyRMBGiftsReward } from './activity/dailyRMBGiftsService';
import { makePopUpShopReward } from './activity/popUpShopService';
import { ActivityModel, ActivityModelType } from '../db/Activity';
import { ActivityFirstGiftModel } from '../db/ActivityFirstGift';
import { ServerlistModel } from '../db/Serverlist';
import { makeGrowthFund } from './activity/growthFundService';
import { makeLimitPackageReward } from './activity/limitPackageService';
import { makeShop } from './activity/treasureHuntService';
import { makeSelfServerShop } from './activity/selfServiceShopActivityService';
import { makeRefreshShopReward } from './activity/refreshShopService';
import { ActivityMonopolyModel, ActivityMonopolyModelType } from '../db/ActivityMonopoly';
import { ActivityMonopolyLandModel, ActivityMonopolyLandModelType } from '../db/ActivityMonopolyLand';
import { pinus } from 'pinus';
import { resResult } from '../pubUtils/util';





/**
 * 结算账单 
 * @param localOrderID 本地订单号
 */
export async function makeOrder(localOrderID: string, sid: string, orderInfo: UserOrderModelType) {
    // let orderInfo = await UserOrderModel.findOrder(localOrderID);

    //商品价格信息
    let productInfo = dicRMB.get(orderInfo.productID)


    let productType = productInfo.type;//类型
    let price = orderInfo.price;//下单时的价格
    let payType = orderInfo.payType;//支付类型
    let roleId = orderInfo.roleId;//角色

    const roleInfo = await RoleModel.increaseTotalPay(roleId, price);

    let rewardResult: any = { code: 0, data: null };

    //首冲礼包
    {
        let { activityGroupId } = await ServerlistModel.findByServerId(orderInfo.serverId);
        let activityArray: ActivityModelType[] = await ActivityModel.findOpenActivityByType(activityGroupId, ACTIVITY_TYPE.FIRST_GIFT, new Date())
        for (let obj of activityArray) {
            await ActivityFirstGiftModel.begin(obj.activityId, roleId);
        }
    }


    switch (productType) {
        case ACTIVITY_TYPE.SIGN_IN_VIP:
            {
                rewardResult = await makeSignInVIP(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
                break;
            }
        case ACTIVITY_TYPE.NEW_PLAYER_LIMIT_PACKAGE:
            {
                rewardResult = await makeLimitPackageReward(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
                break;
            }
        case ACTIVITY_TYPE.LIMIT_PACKAGE_SHOP_DAILY:
        case ACTIVITY_TYPE.LIMIT_PACKAGE_SHOP_WEEKLY:
            {
                rewardResult = await makeLimitPackageReward(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
                break;
            }
        case ACTIVITY_TYPE.YUAN_BAO_SHOP:
            {
                rewardResult = await makeYuanbaoShopReward(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
                break;
            }

        case ACTIVITY_TYPE.MONTHLY_TICKET_1:
        case ACTIVITY_TYPE.MONTHLY_TICKET_2:
            {
                rewardResult = await makeMonthlyTicketReward(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
                break;
            }
        case ACTIVITY_TYPE.DAILY_RMB_GIFTS:
            {
                rewardResult = await makeDailyRMBGiftsReward(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
                break;
            }
        case ACTIVITY_TYPE.POP_UP_SHOP://弹出礼包
            {
                rewardResult = await makePopUpShopReward(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID, orderInfo.paramStr)
                break;
            }
        case ACTIVITY_TYPE.GROWTH_FUND_MAIN_VIP: //主线成长基金（高阶）
            {
                rewardResult = await makeGrowthFund(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
                break;
            }
        case ACTIVITY_TYPE.GROWTH_FUND_TOWER_VIP://镇念塔成长基金（高阶）
            {
                rewardResult = await makeGrowthFund(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
                break;
            }
        case ACTIVITY_TYPE.GROWTH_FUND_MAIN_ELITE_VIP://精英成长基金（高阶）
            {
                rewardResult = await makeGrowthFund(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
                break;
            }
        case ACTIVITY_TYPE.TREASURE_HUNT://寻宝奇兵活动-每日物资商店
            {
                rewardResult = await makeShop(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
                break;
            }
        case ACTIVITY_TYPE.SELF_SERVICE_SHOP://自选商店-购买礼包
            {
                rewardResult = await makeSelfServerShop(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
                break;
            }
        case ACTIVITY_TYPE.REFRESH_SHOP://通用的刷新商店（分页，可刷新，限制购买次数，支持rmb与资源兑换）
            {
                if (orderInfo.paramStr) {//大富翁商店结算
                    let paramObj = JSON.parse(orderInfo.paramStr);
                    let activityId = paramObj.activityId;//大富翁活动id；   orderInfo.activityId是大富翁中的商店活动id
                    if (activityId) {
                        let playerMonopolyData: ActivityMonopolyModelType = await ActivityMonopolyModel.findData(orderInfo.serverId, activityId, roleId);
                        let playerLandData: ActivityMonopolyLandModelType = await ActivityMonopolyLandModel.findDataByPosition(orderInfo.serverId, activityId, roleId, playerMonopolyData.curPosition);
                        let roundIndex = playerLandData.stopCount;
                        rewardResult = await makeRefreshShopReward(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID, roundIndex)
                        break;
                    }
                }
                //正常商店结算
                rewardResult = await makeRefreshShopReward(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID, 0)
                break;
            }
        default:
            rewardResult = STATUS.ERROR_TYPE;
            break;
    }
    pinus.app.get('channelService').pushMessageByUids('onPlayerDataChange', resResult(STATUS.SUCCESS, {
        totalPay: roleInfo.totalPay
    }), [{ uid: roleId, sid }]);
    
    return rewardResult
}