using Microsoft.AspNetCore.Http;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudGaming.Code.AppExtend
{
    /// <summary>
    /// app 请求信息
    /// </summary>
    public class AppRequestConfig(HttpRequest? httpRequest)
    {
        private string channel { get; set; }
        /// <summary>
        /// 渠道
        /// </summary>
        public string Channel
        {
            get
            {
                if (string.IsNullOrEmpty(channel))
                {
                    if (!(httpRequest?.Headers?.TryGetValue("Channel", out var _channel) ?? false))
                    {
                        _channel = "27001";
                    }
                    channel = _channel;
                    if (string.IsNullOrEmpty(channel))
                    {
                        channel = "27001";
                    }
                }
                return channel;
            }
        }

        private string platform;

        /// <summary>
        /// 平台
        /// </summary>
        public string Platform
        {
            get
            {
                if (string.IsNullOrEmpty(platform))
                {
                    if (!(httpRequest?.Headers.TryGetValue("Platform", out var _platform) ?? false))
                    {
                        _platform = "android";
                    }
                    platform = _platform;
                }
                return platform;
            }
        }

        private string version;
        /// <summary>
        /// 版本
        /// </summary>
        public string Version
        {
            get
            {
                if (string.IsNullOrEmpty(version))
                {
                    if (!(httpRequest?.Headers.TryGetValue("Version", out var _version) ?? false))
                    {
                        _version = "1.0.0";
                    }
                    version = _version;
                }
                return version;
            }
        }

        public int versionNum;
        public int VersionNum
        {
            get
            {
                if (versionNum == 0)
                {
                    var str = Version.Replace(".", "");
                    if (!int.TryParse(str, out versionNum))
                    {
                        versionNum = 100;
                    }
                }
                return versionNum;
            }
        }
        private string language;
        /// <summary>
        /// 语言
        /// </summary>
        public string Language
        {
            get
            {
                if (string.IsNullOrEmpty(language))
                {
                    if (!(httpRequest?.Headers.TryGetValue("Language", out var _language) ?? false))
                    {
                        _language = "zh";
                    }
                    language = _language;
                }
                return language;
            }
        }

        /// <summary>
        /// 获取key
        /// </summary>
        public string Key
        {

            get => $"{Language}_{Channel}_{Version}";
        }

        /// <summary>
        /// 地区
        /// </summary>
        public string Continent { get; set; }

        /// <summary>
        /// 国家
        /// </summary>
        public string CountryName { get; set; }
    }
}
