using Foresight.ServiceModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace IronIntel.Contractor
{
    public class MachineDetailWorkspace
    {
        private const string MachineDetailWSP = "MachineDetailWSP";
        private const string Path = "fic/FIC.aspx?IID={0}&wspivots={1}";

        public static string GenerateMachineDetailWSPURL(string machineid, string vin, out int openMode)
        {
            var config = GetConfig();
            openMode = config.OpenMode;
            List<WorkspacePivotItem> pvs = new List<WorkspacePivotItem>();
            if (!string.IsNullOrEmpty(config.MachineIDPivotIID))
            {
                WorkspacePivotItem pv = new WorkspacePivotItem();
                pv.iid = config.MachineIDPivotIID;
                pv.vals = new string[] { machineid };
                pvs.Add(pv);
            }
            if (!string.IsNullOrEmpty(config.VINPivotIID))
            {
                WorkspacePivotItem pv = new WorkspacePivotItem();
                pv.iid = config.VINPivotIID;
                pv.vals = new string[] { vin };
                pvs.Add(pv);
            }

            string pivotString = JsonConvert.SerializeObject(pvs);
            pivotString = System.Web.HttpUtility.UrlEncode(pivotString);
            string url = string.Format(Path, config.WorkspaceIID, pivotString);
            return url;
        }

        public static MachineDetailWorkspaceConfig GetConfig()
        {
            MachineDetailWorkspaceConfig config = new MachineDetailWorkspaceConfig();
            string value = SystemParams.GetStringParam(MachineDetailWSP, false);
            //wspid=00000000-0000-0000-0000-000000000000&pivotid=00000000-0000-0000-0000-000000000000&openmode=0
            if (!string.IsNullOrEmpty(value))
            {
                string[] temps = value.Split('&');
                foreach (string temp in temps)
                {
                    string[] kv = temp.Split('=');
                    if (kv.Length == 2)
                    {
                        if (string.Equals("wspid", kv[0], StringComparison.OrdinalIgnoreCase))
                            config.WorkspaceIID = kv[1];
                        else if (string.Equals("midpivotid", kv[0], StringComparison.OrdinalIgnoreCase))
                            config.MachineIDPivotIID = kv[1];
                        else if (string.Equals("vinpivotid", kv[0], StringComparison.OrdinalIgnoreCase))
                            config.VINPivotIID = kv[1];
                        else if (string.Equals("openmode", kv[0], StringComparison.OrdinalIgnoreCase))
                        {
                            int v = 0;
                            if (int.TryParse(kv[1], out v))
                                config.OpenMode = v;
                        }
                    }
                }
            }
            return config;
        }

        private static void SetConfig(MachineDetailWorkspaceConfig config)
        {
            config.OpenMode = GetConfig().OpenMode;//OpenMode暂不修改
            string value = string.Format("wspid={0}&midpivotid={1}&vinpivotid={2}&openmode={3}", config.WorkspaceIID, config.MachineIDPivotIID, config.VINPivotIID, config.OpenMode);
            SystemParams.SetStringParam(MachineDetailWSP, value);
        }
    }

    public class MachineDetailWorkspaceConfig
    {
        public string WorkspaceIID { get; set; }
        public int OpenMode { get; set; }
        public string MachineIDPivotIID { get; set; }
        public string VINPivotIID { get; set; }
    }

    public class WorkspacePivotItem
    {
        public string iid { get; set; }
        public string[] vals { get; set; }
    }
}
