using System;
using Server.Core;

namespace Server.AliyunSDK.SLSLog
{
    /// <summary>
    /// 为不在多余使用日志库， 留存数据和活跃数据一起存储，使用PeriodType来区分
    /// </summary>
    public class StatisticsRetentionLogData : SLSLogData
    {
        public PeriodType PeriodType;
        public DateTime TargetDate; // 周期起始日期
        public int Platform; // 平台
        public string Channel;  // 渠道
        // ============= 留存数据 =============
        public int RetentionPeriod; // 留存跨度 
        public int NewUsers; // 新增用户
        public int RetainedUsers; // 留存用户
        public double RetainedUsersRate; // 留存率
        // ============= 活跃数据 =============
        public int ActivityUsers; // 活跃用户 

        // ============ 查询数据 =============
        [SLSIgnore]
        public DateTime GroupTime;
        
        public static StatisticsRetentionLogData Create(
            int platform,
            string channel,
            DateTime targetDate, 
            PeriodType periodType,
            int newUsers,
            int period,
            int retainedUsers)
        {
            StatisticsRetentionLogData data = ReferencePool.Fetch<StatisticsRetentionLogData>();
            data.PeriodType = periodType;
            data.TargetDate = targetDate;
            data.Platform = platform;
            data.Channel = channel;
            data.RetentionPeriod = period;
            data.NewUsers = newUsers;
            data.RetainedUsers = retainedUsers;
            data.RetainedUsersRate = newUsers > 0 ? Math.Round((float)retainedUsers / newUsers, 4) : 0;
            data.Time = targetDate.Date + new TimeSpan(23, 59, 59);
            return data;
        }

        public static StatisticsRetentionLogData CreateActivity(
            int platform,
            string channel,
            DateTime targetDate,
            int activityUsers,
            int newUsers)
        {
            StatisticsRetentionLogData data = ReferencePool.Fetch<StatisticsRetentionLogData>();
            data.Platform = platform;
            data.Channel = channel;
            data.PeriodType = PeriodType.Activity;
            data.TargetDate = targetDate;
            data.ActivityUsers = activityUsers;
            data.NewUsers = newUsers;
            data.Time = targetDate.Date + new TimeSpan(23, 59, 59);
            return data;
        }
    }

    public enum PeriodType
    {
        Day,
        Week,
        Month,
        Activity, // 活跃
    }
}