using FI.FIC;
using Foresight.Fleet.Services.Styles;
using IronIntel.Contractor.iisitebase;
using IronIntel.Contractor.Users;
using System;
using System.Drawing;
using System.Web;
using System.Web.UI;

namespace IronIntel.Contractor.Site
{
    public class CommonBase : MasterPage
    {
        protected string CanExportFile = "false";
        protected string StyleVariables = "";

        protected virtual bool ExportModule => false;

        protected virtual StyleInfo GetUIStyle()
        {
            var sessionid = IronIntelBasePage.GetLoginSessionID(Request);
            var user = UserManagement.GetUserBySessionID(sessionid);
            CustUIStyle style = null;
            if (user != null)
            {
                if (ExportModule)
                {
                    CanExportFile = FICHostEnvironment.CanExportToFile(user.IID).ToString().ToLower();
                }
                style = SystemParams.GetUIStyle(user.IID);
                string color;
                if (style != null)
                {
                    color = style.TitleBarColor;
                }
                else
                {
                    color = "#f78e1e";
                }
                string opacity;
                string fore;
                string ctrlbgcolor = "lightgray";
                try
                {
                    var c = ColorTranslator.FromHtml(color);
                    opacity = string.Format("rgb({0} {1} {2}/60%)", c.R, c.G, c.B);
                    var g = .299 * c.R + .587 * c.G + .114 * c.B;
                    fore = g < 127.5 ? "#f0f0f0" : "#0f0f0f";
                    ctrlbgcolor = g < 221 ? color : "lightgray";//221为light的计算值
                }
                catch
                {
                    opacity = "rgb(247 142 30/60%)";
                    fore = "#0f0f0f";
                    ctrlbgcolor = "lightgray";
                }
                StyleVariables = $"--title-color: {fore}; --title-bg-color: {color}; --title-bg-opacity-color: {opacity};--title-ctrlbg-color: {ctrlbgcolor}; ";
            }
            return new StyleInfo
            {
                User = user,
                Style = style
            };
        }

        protected string GetUrl(string file)
        {
            string url;
            Page page = HttpContext.Current.Handler as Page;
            if (page != null)
            {
                // Use page instance.
                url = page.ResolveUrl("~/") + file;
            }
            else
            {
                // avoid duplicate operation
                url = HttpContext.Current.Request.ApplicationPath + "/" + file;
            }
            try
            {
                var path = System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, file);
                if (System.IO.File.Exists(path))
                {
                    url += "?t=" + System.IO.File.GetLastWriteTimeUtc(path).Ticks;
                }
            }
            catch (Exception)
            {
                // cant read file
            }
            return url;
        }
    }

    public class StyleInfo
    {
        public UserInfo User { get; set; }
        public CustUIStyle Style { get; set; }
    }
}
