using System.Net.Http;
using System.Net;
using Zerolauncher.Defender;
using Zerolauncher.util;
using System.Diagnostics;

namespace Zerolauncher.Manager
{
    class StaticUrl
    {
        public const string QMessageMain = "https://sharechain.qq.com/";
        public const string MasterMessage = "54aeb77c47b967d22c2c1396d00d91f0";
        public const string SummerMessage = "037846c482eddc948612b0d0f8ed98d5";
        public const string LoveMessage = "539e3389078daa765ac0abdcc97eebce";
        public const string TWMasterMessage = "3ca068a2cdb1ee85fc1152ff6cabefcd";
    }

    class UpDateData
    {
        public static bool is_check = false;
        public static bool state = false;
        public static string version = "";
        public static string tis = "";
        public static string auto_packet_url = "";
        public static string user_packet_url = "";
        public const string lanzou = "https://www.lanzoub.com/";
    }

    class CloundMananger
    {
        public static async Task TakeQMessage()
        {
            var client = new HttpClient();
            client.DefaultRequestVersion = HttpVersion.Version20;
            client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
            client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (iPad; U; CPU OS 6_0 like Mac OS X; zh-CN; iPad2)");
            HttpResponseMessage response;
            try
            {
                client.Timeout = TimeSpan.FromMinutes(3);
                response = await client.GetAsync($"{StaticUrl.QMessageMain}{StaticUrl.TWMasterMessage}");
            }
            catch (Exception _ex)
            {
                CacheSha.errorCode = 1;
                return;
            }
            var responseString = await response.Content.ReadAsStringAsync();
            if(!FormatMessage(responseString))
            {
                CacheSha.errorCode = 2;
            }
            UpDateData.is_check = true;
        }

        static bool FormatMessage(string message)
        {
            message = message.Split("<div class=\"note-content\">")[1].Split("</article>")[0].Replace("</div>", "").Replace("\n", "").Replace("\t", "").Replace("\r", "").Replace("'", "");
            var arr = message.Split("<div>");
            Trace.WriteLine("bababababa" + arr.Length.ToString());
            Trace.WriteLine(message);
            if (arr.Length == 7 && FormatSha(arr[5], true) && FormatSha(arr[6], false))
            {
                Trace.WriteLine("bababababa" + arr.Length.ToString());
                UpDateData.version = arr[1];
                UpDateData.tis = arr[2];
                UpDateData.auto_packet_url = arr[3];
                UpDateData.user_packet_url = arr[4];
                UpDateData.state = true;
                return true;
            }
            return false;
        }

        static bool FormatSha(string sha0, bool e)
        {
            //if (sha0.Length < 80) return false;
            int count;
            var sha = SpaceEncoder.Decode(sha0, out count);
            if (sha == null || sha.Length < 64)
            {
                Trace.WriteLine($"faile: {count}");
                return false;
            }
            if (e) CacheSha.PutE(sha);
            else CacheSha.PutM(sha);
            Trace.WriteLine($"isE-{e},succ: {sha}");
            return true;
        }
    }
}
