using UnityEngine;
using System;
using Touka.GameLogic;

/// <summary>
/// SDK内部工具类
/// </summary>
public class ToukaUtilsInner : ToukaSingletonMonoBehaviour<ToukaUtilsInner>
{
    /// <summary>
    /// 是否为首日登录 - 自然日
    /// </summary>
    public bool isFirstDayLogin = false;

    /// <summary>
    /// 保存首次登录时间
    /// </summary>
    public void SaveFirstLoginTime()
    {
        if(ToukaUtils.IfFirstCheckPlayerPrefs(StaticStringsPlayerPrefs.FirstLoginFlag))
        {
            ToukaUtils.SaveCurrTime2PlayerPrefs(StaticStringsPlayerPrefs.FirstLoginTime);
        }
    }

    /// <summary>
    /// 次日登录 - 自然日
    /// </summary>
    public void Check2DaysLogin()
    {
        var loginCountKey = StaticStringsPlayerPrefs.ToukaLoginCount;
        var loginCount = ToukaUtils.GetPlayerPrefsIntByKey(loginCountKey);
        if (loginCount == 2)
        {
            Debug.Log("已经符合条件上报过了，不进行后续判断");
            return;
        }

        isFirstDayLogin = true;    

        var todayKey = DateTime.Today.DayOfYear.ToString();
        Debug.Log("todayKey : " + todayKey);
        if (ToukaUtils.GetPlayerPrefsIntByKey(todayKey) == 0)
        {
            Debug.Log("今天为首次登陆");
            ToukaUtils.SavePlayerPrefsIntByKeyValue(todayKey, 1);

            var lastDay = (DateTime.Today.DayOfYear - 1).ToString();
            Debug.Log("lastDay : " + lastDay);
            if (loginCount == 1 && ToukaUtils.GetPlayerPrefsIntByKey(lastDay) == 1)
            {
                Debug.Log("次日启动：之前没有上报过，符合条件。可以上报");
                isFirstDayLogin = false;
                ToukaAnalyticsManager.Instance.LogEvent(ToukaLogType.Tenjin, StaticStringsEvent.Event_Tenjin_TKInner_NewUserNextDayLogin);
            }
            Debug.Log("本地保存登陆天数+1");
            ToukaUtils.SavePlayerPrefsIntByKeyValue(loginCountKey, loginCount + 1);
        }

        if (isFirstDayLogin)
        {
            Debug.Log("是第一天登录");
        }
        else
        {
            Debug.Log("不 是第一天登录");
        }
    }
}
