using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SLC1_N
{
    public static class ActivationManager
    {
        private static readonly string ActivationFilePath;

        static ActivationManager()
        {
            // 设置激活文件路径
            string userFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            ActivationFilePath = Path.Combine(userFolder, "AppData", "Local", "mxlSoftware", "activation.bin");

            // 确保目录存在
            Directory.CreateDirectory(Path.GetDirectoryName(ActivationFilePath));
        }

        // 写入激活状态和到期时间
        public static void WriteActivationStatus(bool isActivated, DateTime expiryTime)
        {
            try
            {
                string content = $"{isActivated}|{expiryTime:yyyy-MM-dd HH:mm:ss}";
                File.WriteAllText(ActivationFilePath, content);
            }
            catch (Exception ex)
            {
                
            }
        }

        // 写入激活状态并设置有效期
        public static void WriteActivationStatus(bool isActivated, int validDays)
        {
            DateTime expiryTime = isActivated ? DateTime.Now.AddDays(validDays) : DateTime.MinValue;
            WriteActivationStatus(isActivated, expiryTime);
        }

        // 只写入激活状态
        public static void WriteActivationStatus(bool isActivated)
        {
            DateTime expiryTime = isActivated ? DateTime.MaxValue : DateTime.MinValue;
            WriteActivationStatus(isActivated, expiryTime);
        }

        // 读取激活状态和到期时间
        public static (bool IsActivated, DateTime ExpiryTime) ReadActivationStatus()
        {
            try
            {
                if (!File.Exists(ActivationFilePath))
                {
                    return (false, DateTime.MinValue);
                }

                string content = File.ReadAllText(ActivationFilePath);

                if (!content.Contains('|'))
                {
                    bool status = bool.Parse(content);
                    DateTime etime = status ? DateTime.MaxValue : DateTime.MinValue;
                    return (status, etime);
                }

                string[] parts = content.Split('|');
                bool isActivated = bool.Parse(parts[0]);
                DateTime expiryTime = DateTime.ParseExact(parts[1], "yyyy-MM-dd HH:mm:ss", null);

                return (isActivated, expiryTime);
            }
            catch (Exception ex)
            {
                return (false, DateTime.MinValue);
            }
        }

        // 只读取激活状态
        public static bool ReadActivationStatusSimple()
        {
            var (isActivated, _) = ReadActivationStatus();
            return isActivated;
        }

        // 检查激活是否过期
        public static bool CheckActivationExpiry()
        {
            var (isActivated, expiryTime) = ReadActivationStatus();

            if (!isActivated)
                return false;

            // 检查是否过期
            return DateTime.Now >= expiryTime;
        }

        // 获取剩余时间
        public static TimeSpan GetRemainingTime()
        {
            var (isActivated, expiryTime) = ReadActivationStatus();

            if (!isActivated || DateTime.Now > expiryTime)
                return TimeSpan.Zero;

            return expiryTime - DateTime.Now;
        }

        // 获取剩余天数
        public static double GetRemainingDays()
        {
            TimeSpan remaining = GetRemainingTime();
            return remaining.TotalDays;
        }

        // 获取剩余小时数
        public static double GetRemainingHours()
        {
            TimeSpan remaining = GetRemainingTime();
            return remaining.TotalHours;
        }

        // 判断是否是永久激活
        public static bool IsPermanentActivation()
        {
            var (isActivated, expiryTime) = ReadActivationStatus();
            return isActivated && expiryTime == DateTime.MaxValue;
        }

        // 重置激活状态（删除文件）
        public static void ResetActivation()
        {
            try
            {
                if (File.Exists(ActivationFilePath))
                {
                    File.Delete(ActivationFilePath);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"重置激活状态失败: {ex.Message}");
            }
        }
    }
}

/*
 * 
// 写入激活状态和具体到期时间
ActivationManager.WriteActivationStatus(true, new DateTime(2024, 12, 31));

// 写入激活状态并设置30天有效期
ActivationManager.WriteActivationStatus(true, 30);

// 写入永久激活
ActivationManager.WriteActivationStatus(true);

// 读取激活状态和到期时间
var (isActivated, expiryTime) = ActivationManager.ReadActivationStatus();

// 检查是否过期
bool isValid = ActivationManager.CheckActivationExpiry();

// 获取剩余天数
double remainingDays = ActivationManager.GetRemainingDays();

// 检查是否是永久激活
bool isPermanent = ActivationManager.IsPermanentActivation();
 */
