import { ACTIVITY_TYPE, STATUS, TASK_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityGrowthFundModel, ActivityGrowthFundModelType } from '../../db/ActivityGrowthFund';
import { ActivityBuyRecordsModel } from '../../db/ActivityBuyRecords';
import { RoleModel } from '../../db/Role';
import { GrowthFundData } from '../../domain/activityField/growthFundField';
import { ServerlistModel } from '../../db/Serverlist';
import { getActivitiesByType, getActivityById } from './activityService';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';

/**
   * 获取活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} type 活动类型 ACTIVITY_TYPE
   * @param {string} roleId 角色Id
   *
   */
/*ACTIVITY_TYPE类型
  GROWTH_FUND_MAIN = 6, // 主线成长基金
  GROWTH_FUND_MAIN_VIP = 7, // 主线成长基金（高阶）
  GROWTH_FUND_TOWER = 8, // 镇念塔成长基金
  GROWTH_FUND_TOWER_VIP = 9, // 镇念塔成长基金（高阶）
  GROWTH_FUND_MAIN_ELITE = 10, // 精英成长基金
  GROWTH_FUND_MAIN_ELITE_VIP = 11, // 精英成长基金（高阶）
*/
export async function growthFundActivity(type: number, serverId: number, roleId: string) {
    let activityArray = await getActivitiesByType(serverId, type);
    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0]
    let playerData = await getPlayerGrowthFundData(activityData.activityId, serverId, roleId);
    if (playerData.isComplete()) {
        return null;
    }
    return playerData;
}

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerGrowthFundData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let playerRecords: ActivityGrowthFundModelType[] = await ActivityGrowthFundModel.findData(activityId, roleId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new GrowthFundData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerRecords);
    if (playerData.isVipActivity()) {//vip高阶需要购买
        let buyRecords = await ActivityBuyRecordsModel.findRecordsByActivityId(activityData.activityId, roleId);
        playerData.initBuyRecords(buyRecords);
    }

    let activityType = activityData.type;
    switch (activityType) {
        case ACTIVITY_TYPE.GROWTH_FUND_MAIN://主线
        case ACTIVITY_TYPE.GROWTH_FUND_MAIN_VIP://主线vip
            {
                let role = await RoleModel.findByRoleId(roleId);
                let { warStar = new Array<{ id: number, warType: number, star: number }>() } = role;
                let warIDs = warStar.map(cur => cur.id);
                for (let page of playerData.list) {
                    for (let item of page.items) {
                        if (item.taskType == TASK_TYPE.BATTLE_MAIN) {
                            let index = warIDs.findIndex(id => { return id === item.taskParamArray[1] })
                            if (index != -1) {
                                item.isComplete = true;
                            }
                        }
                    }
                }
                break;
            }
        case ACTIVITY_TYPE.GROWTH_FUND_TOWER://镇念塔
        case ACTIVITY_TYPE.GROWTH_FUND_TOWER_VIP:////镇念塔vip 
            {
                let { towerLv } = await RoleModel.findByRoleId(roleId);
                for (let page of playerData.list) {
                    for (let item of page.items) {
                        if (item.taskType == TASK_TYPE.BATTLE_TOWER_LV) {
                            if (towerLv >= item.condition) {
                                item.isComplete = true;
                            }
                        }
                    }
                }
                break;
            }
        case ACTIVITY_TYPE.GROWTH_FUND_MAIN_ELITE: //精英
        case ACTIVITY_TYPE.GROWTH_FUND_MAIN_ELITE_VIP: //精英VIP
            {
                let role = await RoleModel.findByRoleId(roleId);
                let { warStar = new Array<{ id: number, warType: number, star: number }>() } = role;
                let warIDs = warStar.map(cur => cur.id);
                for (let page of playerData.list) {
                    for (let item of page.items) {
                        if (item.taskType == TASK_TYPE.BATTLE_MAIN_ELITE) {
                            let index = warIDs.findIndex(id => { return id === item.taskParamArray[1] })
                            if (index != -1) {
                                item.isComplete = true;
                            }
                        }
                    }
                }
                break;
            }
        default:
            break;
    }
    return playerData;
}




/**
   * 购买高阶版
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   * @param {string} productID 商品ID
   *
   */
export async function makeGrowthFund(roleId: string, roleName: string, sid: string, serverId: number,
    activityId: number, productID: string) {
    let activityData: ActivityModelType = await getActivityById(activityId);
    if (!activityData) {
        return STATUS.ACTIVITY_MISSING;
    }
    if (activityData.type !== ACTIVITY_TYPE.GROWTH_FUND_MAIN_VIP && activityData.type !== ACTIVITY_TYPE.GROWTH_FUND_TOWER_VIP && activityData.type !== ACTIVITY_TYPE.GROWTH_FUND_MAIN_ELITE_VIP) {
        return STATUS.ACTIVITY_TYPE_ERROR;
    }
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new GrowthFundData(activityData, createTime, serverTime);
    let pageData = playerData.findPageByProductID(productID);
    let pageIndex = pageData.pageIndex;
    await ActivityBuyRecordsModel.addRecord(activityId, roleId, activityData.type, pageIndex, true);
    return {
        code: 0,
        data: Object.assign({}, { item: { pageIndex }, activityId: activityId })
    }
}