import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityBindPhoneRewardModel } from '../../db/ActivityBindPhoneReward';
import { ActivityDailyChallengesModel, ActivityDailyChallengesModelType } from '../../db/ActivityDailyChallenges';
import { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { UserModel } from '../../db/User';
import { BindPhoneData } from '../../domain/activityField/bindPhoneField';
import { DailyChallengesData } from '../../domain/activityField/dailyChallengesField';
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 getBindPhoneData(activityId: number, roleId: string, serverId: number, uid: number) {

    let activityData: ActivityModelType = await getActivityById(activityId);
    if(!activityData || activityData.type != ACTIVITY_TYPE.BIND_PHONE) return null;
    
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new BindPhoneData(activityData, createTime, serverTime);

    let user = await UserModel.findUserByUid(uid);
    let receiveRec = await ActivityBindPhoneRewardModel.findByUid(activityId, uid);
    playerData.setRecord(user, receiveRec);

    return playerData;
}


/**
   * 玩家玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getBindPhoneDataShow(activityId: number, roleId: string, serverId: number, uid: number) {
    let playerData = await getBindPhoneData(activityId, roleId, serverId, uid);
    if(playerData && playerData.canShow && playerData.canShow()) {
        return playerData.getShowResult();
    }
    return null
}


