using System;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MySystem.Models;
using Library;

namespace MySystem.Areas.Api.Controllers
{
    public class BaseController : Controller
    {
        public IHttpContextAccessor _accessor;
        public readonly ILogger<BaseController> _logger;
        public readonly IOptions<Setting> _setting;
        public Setting AppSetting;

        public WebCMSEntities db = new WebCMSEntities();

        public string ApiKey = "G6H7@J8%";
        public string Host = Library.ConfigurationManager.AppSettings["Host"].ToString();
        public string SourceHost = Library.ConfigurationManager.AppSettings["SourceHost"].ToString();
        public string Database = Library.ConfigurationManager.AppSettings["Database"].ToString();
        public string defaultImage = Library.ConfigurationManager.AppSettings["Host"].ToString() + "/skin/app/default/static/images/header.png";

        //jwt参数
        public string JwtSecret = Library.ConfigurationManager.AppSettings["JwtSecret"].ToString();
        public string JwtIss = Library.ConfigurationManager.AppSettings["JwtIss"].ToString();
        public string JwtAud = Library.ConfigurationManager.AppSettings["JwtAud"].ToString();

        public int SysId;
        public int SysRealRole;
        public string SysUserName = "";
        public string SysRealName = "";
        public string RightInfo = "";

        public BaseController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting)
        {
            _accessor = accessor;
            _logger = logger;
            _setting = setting;
            AppSetting = setting.Value;
            Host = AppSetting.Host;
            Database = AppSetting.Database;
        }

        #region 接口通用DES解密

        public string DesDecrypt(string content)
        {
            content = HttpUtility.UrlDecode(content);
            return dbconn.DesDecrypt(content, "*ga34|^7");
        }

        #endregion

        #region 根据数字获取订单状态名称

        public string getOrderStatus(int status)
        {
            string result = "";
            switch (status)
            {
                case -1:
                    result = "已取消";
                    break;
                case 0:
                    result = "未付款";
                    break;
                case 1:
                    result = "待发货";
                    break;
                case 2:
                    result = "待确认";
                    break;
                case 3:
                    result = "已完成";
                    break;
                case 4:
                    result = "已评价";
                    break;
                case 5:
                    result = "退款中";
                    break;
                case 6:
                    result = "已退款";
                    break;
                default: break;
            }
            return result;
        }

        #endregion

        #region 根据数字获取报名状态名称

        public string getSignUpStatus(int status)
        {
            string result = "";
            switch (status)
            {
                case -1:
                    result = "已取消";
                    break;
                case 0:
                    result = "未付款";
                    break;
                case 1:
                    result = "已付款";
                    break;
                default: break;
            }
            return result;
        }

        #endregion

        #region 根据数字获取配送方式

        public string getMeterMode(int num)
        {
            string result = "";
            switch (num)
            {
                case 1:
                    result = "快递";
                    break;
                case 2:
                    result = "EMS";
                    break;
                case 3:
                    result = "平邮";
                    break;
                default:
                    break;
            }
            return result;
        }

        #endregion

        #region 两点距离

        public double GetDistanceNumber(string start, string end)
        {
            if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
            {
                string[] startpos = start.Split(',');
                string[] endpos = end.Split(',');
                double lng1 = double.Parse(startpos[0]);
                double lat1 = double.Parse(startpos[1]);
                double lng2 = double.Parse(endpos[0]);
                double lat2 = double.Parse(endpos[1]);
                double radLat1 = rad(lat1);
                double radLat2 = rad(lat2);
                double a = radLat1 - radLat2;
                double b = rad(lng1) - rad(lng2);
                double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
                s = s * EARTH_RADIUS;
                s = Math.Round(s * 10000) / 10000;
                return s;
            }
            return 10000000;
        }
        private double rad(double d)
        {
            return d * Math.PI / 180.0;
        }
        private double EARTH_RADIUS = 6378.137;

        #endregion

        #region 默认图片

        public string DefaultPic(string picpath)
        {
            if (!string.IsNullOrEmpty(picpath))
            {
                if (picpath.StartsWith("http"))
                {
                    return picpath;
                }
                return SourceHost + picpath;
            }
            return Host + "default.png";
        }

        #endregion

        #region 解析编辑器中的图片

        public string CheckContentImage(string content)
        {
            if (string.IsNullOrEmpty(content))
            {
                return "";
            }
            MatchCollection mc = Regex.Matches(content, "<img.*?/>");
            foreach (Match match in mc)
            {
                Match m = Regex.Match(match.Value, "src=\".*?\"");
                if (m.Success)
                {
                    string srcString = m.Value;
                    string picPath = SourceHost + srcString.Replace("src=\"", "").Replace("\"", "");
                    content = content.Replace(srcString, "src=\"" + picPath + "\"");
                }
            }
            return content;
        }

        #endregion

    }
}