using JiaZhiQuan.Common.Models.VO.CreatorCenter;
using System;
using System.Collections.Generic;
using System.Linq;

namespace JiaZhiQuan.Common.Utils
{
    /// <summary>
    /// 用户留存
    /// </summary>
    public class UserRetainedUtils
    {

        /// <summary>
        /// 获取用户的天留存状态
        /// </summary>
        /// <param name="userRetainedList">用户的流程记录</param>
        /// <param name="day">第几日的留存</param>
        /// <param name="userCreateAt">用户的创建时间</param>
        /// <returns></returns>
        public static string UserDayRetainedStateText(List<UserDayRetainedDTO> userRetainedList, int day, DateTime userCreateAt)
        {
            int retainedDay = 0;
            string userCreateDateStr = userCreateAt.ToString("yyyy-MM-dd"),
                   nowDateStr = DateTime.Now.Date.ToString("yyyy-MM-dd");
            if (userCreateDateStr != nowDateStr)
            {
                DateTime userCreateDate = DateTime.Parse(userCreateDateStr);
                TimeSpan timeDifference = DateTime.Now.Date - userCreateDate;
                // 计算创建时间距离今天过去了多少天
                retainedDay = (int)Math.Ceiling(timeDifference.TotalDays);
            }
            // 说明是今天刚创建的
            if (retainedDay == 0) return "";
            // 如果获取的留存天数>用户创建后过去的天数
            if (day > retainedDay) return "";

            return userRetainedList.Exists(e => e.day == day) ? "是" : "否";
        }

        /// <summary>
        /// 获取用户的月留存状态
        /// </summary>
        /// <param name="userRetainedList">用户的流程记录</param>
        /// <param name="day">第几日的留存</param>
        /// <param name="userCreateAt">用户的创建时间</param>
        /// <returns></returns>
        public static string UserMonthRetainedStateText(List<UserMonthRetainedDTO> userRetainedList, int month, DateTime userCreateAt)
        {
            int retainedMonth = 0;
            string userCreateDateStr = userCreateAt.ToString("yyyy-MM-01"),
                   nowDateStr = DateTime.Now.Date.ToString("yyyy-MM-01");
            if (userCreateDateStr != nowDateStr)
            {
                retainedMonth = DateTimeUtils.TwoDateDiffMonths(userCreateDateStr, nowDateStr);
            }
            // 说明是当前月刚创建的
            if (retainedMonth == 0) return "";
            // 如果获取的留存月数>用户创建后过去的月数
            if (month > retainedMonth) return "";

            return userRetainedList.Exists(e => e.month == month) ? "是" : "否";
        }

    }

    public class UserDayRetainedDTO
    {
        public int id { get; set; }
        public long userId { get; set; }
        public string appSource { get; set; }
        public int day { get; set; }
        public string activeDate { get; set; }
    }

    public class UserMonthRetainedDTO
    {
        public int id { get; set; }
        public DateTime userCreateTime { get; set; }
        public long userId { get; set; }
        public string appSource { get; set; }
        public int month { get; set; }
        public string monthFirstDay { get; set; }
    }
}
