import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityMonopolyModel, ActivityMonopolyModelType } from '../../db/ActivityMonopoly';
import { ActivityMonopolyLandModel, ActivityMonopolyLandModelType } from '../../db/ActivityMonopolyLand';
import { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { MonopolyData } from '../../domain/activityField/monopolyField';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';
import { getActivitiesByType, getActivityById } from './activityService';


/**
   * 获取活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getMonopolyActivity(serverId: number, roleId: string) {
    let activityDataArray = await getActivitiesByType(serverId, ACTIVITY_TYPE.MONOPOLY);
    if (activityDataArray.length === 0) {
        return null;
    }
    let activityData: ActivityModelType = activityDataArray[0];
    let playerMonopolyData: ActivityMonopolyModelType = await ActivityMonopolyModel.findData(serverId, activityData.activityId, roleId);
    let playerLandData: ActivityMonopolyLandModelType[] = await ActivityMonopolyLandModel.findData(serverId, activityData.activityId, roleId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new MonopolyData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerMonopolyData, playerLandData);
    return playerData;
}


/**
   * 玩家玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerMonopolyData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    if (!activityData) {
        return null;
    }
    let playerMonopolyData: ActivityMonopolyModelType = await ActivityMonopolyModel.findData(serverId, activityData.activityId, roleId);
    let playerLandData: ActivityMonopolyLandModelType[] = await ActivityMonopolyLandModel.findData(serverId, activityData.activityId, roleId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new MonopolyData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerMonopolyData, playerLandData);
    return playerData;
}

/**
   * 玩家玩家活动数据
   *
   * @param {number} position 当前位置
   * @param {number} addStep 移动步数
   * @param {number} max 最大位置
   *
   */
export function nextPosition(position: number, addStep: number, max: number) {
    let temp = (position + addStep) % max;
    return (temp == 0) ? max : temp;
}

