import moment = require('moment');
import { ACTIVITY_TYPE, SERVER_OPEN_TIME, SIGNIN_VIP_OPEN_LIMIT, STATUS } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivitySignInModel, ActivitySignInModelType } from '../../db/ActivitySignIn';
import { SignInData } from '../../domain/activityField/signInField';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';
import { getActivitiesByType, getActivityById } from './activityService';

/**
   * 获取活动数据
   *
   * @param {number} type 普通、高级 ACTIVITY_TYPE
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function signInActivity(serverId: number, roleId: string) {
    let activityArray = await getActivitiesByType(serverId, ACTIVITY_TYPE.SIGN_IN);
    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0];
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new SignInData(activityData, createTime, serverTime);
    let curDate = moment(new Date()).valueOf();
    if (curDate < playerData.beginTime || curDate > playerData.endTime) {//未开启
        return null;
    }
    let playerRecords: ActivitySignInModelType = await ActivitySignInModel.findData(activityData.activityId, roleId, playerData.roundIndex);
    playerData.setPlayerRecords(playerRecords);
    return playerData;
}

/**
   * 获取活动数据
   *
   * @param {number} type 普通、高级 ACTIVITY_TYPE
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function signInVIPActivity(serverId: number, roleId: string) {
    let activityArray = await getActivitiesByType(serverId, ACTIVITY_TYPE.SIGN_IN_VIP);
    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0];
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new SignInData(activityData, createTime, serverTime);

    let curDate = moment(new Date()).valueOf();
    if (curDate < playerData.beginTime || curDate > playerData.endTime) {//未开启
        return null;
    }

    let playerRecords: ActivitySignInModelType = await ActivitySignInModel.findData(activityData.activityId, roleId, playerData.roundIndex);
    playerData.setPlayerRecords(playerRecords);
    return playerData;
}

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerSignInData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    if (activityData.type !== ACTIVITY_TYPE.SIGN_IN_VIP && activityData.type !== ACTIVITY_TYPE.SIGN_IN && activityData.type !== ACTIVITY_TYPE.NEW_PLAYER_SIGN_IN
        && activityData.type !== ACTIVITY_TYPE.COMMON_SIGN_IN) {
        return null;
    }
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);

    let playerData = new SignInData(activityData, createTime, serverTime);
    let playerRecords: ActivitySignInModelType = await ActivitySignInModel.findData(activityId, roleId, playerData.roundIndex);
    playerData.setPlayerRecords(playerRecords);
    return playerData;
}


/**
   * 购买vip签到
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   * @param {string} productID 商品ID
   *
   */
export async function makeSignInVIP(roleId: string, roleName: string, sid: string, serverId: number, 
    activityId: number, productID: string) {
    let activityData: ActivityModelType = await getActivityById(activityId);
    if (activityData.type !== ACTIVITY_TYPE.SIGN_IN_VIP) {
        return STATUS.ACTIVITY_TYPE_ERROR;
    }
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new SignInData(activityData, createTime, serverTime);

    await ActivitySignInModel.buyVIP(activityId, roleId, playerData.roundIndex);

    return {
        code: 0,
        data: Object.assign({}, { item: { isVip: true }, activityId: activityData.activityId })
    }
}

//高级签到开启条件
export async function canBuyVip(lv: number) {
    //高级签到开启条件
    let openTime = SERVER_OPEN_TIME;
    if (moment(openTime).valueOf() + 24 * 60 * 60 * 1000 * 8 > moment(new Date()).valueOf()) {//开服时间8天后才能开启vip签到
        return false;
    }
    // let { lv } = await RoleModel.findByRoleId(roleId);
    if (lv < SIGNIN_VIP_OPEN_LIMIT) {//玩家等级大于等于15级才能开启vip签到
        return false
    }
    return true;
}

