using DDUtilityApp.DATA;
using DDUtilityApp.WORKFLOW.DATA;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using JWH;

namespace DDUtilityApp.WORKFLOW
{

    public partial class FrmWorkflow : Form
    {

        public FrmWorkflow()
        {
            InitializeComponent();
            this.SetLayout();
            this.SetEventHandler();
        }

        protected void SetLayout()
        {
            this.grid.AddColumn("ModelID");
            this.grid.AddColumn("FileName");
            this.grid.AddColumn("DisplayName");
            this.grid.AddColumn("LibraryName");
            this.grid.AddColumn("ClassName");
            this.grid.AddColumn("MethodName");
            this.grid.AddColumn("ArgumentsText");
        }

        protected void SetEventHandler()
        {
            this.btnSample.Click += BtnSample_Click;
        }

        public Flowchart StartFlowchart { get; set; }

        private void BtnSample_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            //dlg.InitialDirectory = $@"C:\download\";
            if (dlg.ShowDialog() != DialogResult.OK) return;

            XmlDocument document = new XmlDocument();
            document.Load(dlg.FileName);

            XmlNode nodeActivity = document.GetSingleNodeByName("Activity");
            if (nodeActivity == null) return;

            XmlNode nodeFlowchart = nodeActivity.GetSingleNodeByName("Flowchart");
            if (nodeFlowchart == null) return;
            this.StartFlowchart = new Flowchart(nodeFlowchart);
        }

        private void xBtnSample_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() != DialogResult.OK) return;

            XmlDocument document = new XmlDocument();
            document.Load(dlg.FileName);

            XmlNodeList activityNodes = document.GetElementsByTagName($"ead:DynamicInvokeActivity");
            XmlNodeList aInNodes = document.GetElementsByTagName($"ead:DynamicInvokeActivity.Parameters");
            XmlNodeList aOutNodes = document.GetElementsByTagName($"ead:DynamicInvokeActivity.Result");

            List<Workflow> lstWorkflow = new List<Workflow>();
            string libraryName = string.Empty;
            string className = string.Empty;
            string methodName = string.Empty;
            string aName = string.Empty;
            string aType = string.Empty;
            string aValue = string.Empty;

            string aTitle = string.Empty;

            string aData = string.Empty;

            string fnName = string.Empty;
            string fnKey = string.Empty;
            string fnValue = string.Empty;
            string fnData = string.Empty;

            int aCount = 0;
            int aaCount = 0;
            int attributeCount = 0;
            int fnCount = 0;

            
            foreach (XmlNode activityNode in activityNodes)
            {
                libraryName = activityNode.Attributes["LibraryName"].Value;
                className = activityNode.Attributes["ClassName"].Value;
                methodName = activityNode.Attributes["MethodName"].Value;
                aCount = activityNode.ChildNodes.Count;

                StringBuilder sb = new StringBuilder();
                StringBuilder fnsb = new StringBuilder();

                for (int i = 0; i < aCount; i++)
                {
                    aaCount = activityNode.ChildNodes[i].ChildNodes.Count;

                    for (int j = 0; j < aaCount; j++)
                    {
                        attributeCount = activityNode.ChildNodes[i].ChildNodes[j].Attributes.Count;                     
                        aTitle = activityNode.ChildNodes[i].ChildNodes[j].Name;
                        
                        sb.Append($"{aTitle} => ");
                        for (int k = 0; k < attributeCount; k++)
                        {
                            if (activityNode.ChildNodes[i].ChildNodes[j].ChildNodes[0].Attributes != null)
                            { 
                                fnCount = activityNode.ChildNodes[i].ChildNodes[j].ChildNodes.Count;
                            }
                            aName = activityNode.ChildNodes[i].ChildNodes[j].Attributes[k].Name;
                            aValue = activityNode.ChildNodes[i].ChildNodes[j].Attributes[k].Value;
                            aData = activityNode.ChildNodes[i].ChildNodes[j].InnerText;

                            if (fnCount > 0)
                            {
                                for (int m = 0; m < fnCount; m++)
                                {
                                    fnName = activityNode.ChildNodes[i].ChildNodes[j].ChildNodes[m].Name;
                                    fnKey = activityNode.ChildNodes[i].ChildNodes[j].ChildNodes[m].Attributes[m].Name;
                                    fnValue = activityNode.ChildNodes[i].ChildNodes[j].ChildNodes[m].Attributes[m].Value;
                                    fnData = activityNode.ChildNodes[i].ChildNodes[j].ChildNodes[m].InnerText;

                                    fnsb.Append($"({fnName}=> {fnKey}={fnValue} , {fnData})");
                                }
                            }

                            sb.Append($"{aName}=");
                            sb.Append($"{aValue} ");
                        }
                        
                        sb.Append(fnsb.ToString());
                        sb.AppendLine($" {aData} ");

                        string aaa = ($"Name= {aName}, Value= {aValue}");
                    }
                }

                string sba = sb.ToString();

                aName = activityNode.ChildNodes[0].ChildNodes[0].Attributes[0].Name;
                aValue = activityNode.ChildNodes[0].ChildNodes[0].Attributes[0].Value;
                //aName = activityNode.ChildNodes.Item(0).Attributes["x:TypeArguments"].Value;

                Workflow workflow = new Workflow();
                workflow.DisplayName = "";
                workflow.LibraryName = libraryName;
                workflow.ClassName = className;
                workflow.MethodName = methodName;
                workflow.Arguments.Add(new Argument("eisMsg", "Object", "IN_DATA"));
                workflow.Arguments.Add(new Argument("eisMsg", "Object", "IN_DATA"));
                workflow.Arguments.Add(new Argument("eisMsg", "Object", "IN_DATA"));
                lstWorkflow.Add(workflow);
            }

            this.grid.AutoBinding(lstWorkflow.ToArray());
        }

        private Workflow[] GetWorkflows(string fullName)
        {
            string modelID = "SPECTYPE_01_DSENT";
            string fileName = System.IO.Path.GetFileNameWithoutExtension(fullName);
            List<Workflow> lstWorkflow = new List<Workflow>();
            Workflow workflow = null;

            workflow = new Workflow(modelID, fileName);
            workflow.DisplayName = "GetReturnCode";
            workflow.LibraryName = "DDEIS.Common";
            workflow.ClassName = "DDEIS.BaseActivityforB1";
            workflow.MethodName = "GetDDEISMessageReturnCode";
            workflow.Arguments.Add(new Argument("eisMsg", "Object", "IN_DATA"));
            lstWorkflow.Add(workflow);

            workflow = new Workflow(modelID, fileName);
            workflow.DisplayName = "UpdateEismsgName";
            workflow.LibraryName = "DDEIS.Common";
            workflow.ClassName = "DDEIS.BaseActivityforB1";
            workflow.MethodName = "UpdateDDEISMessageName";
            workflow.Arguments.Add(new Argument("eisMsg", "Object", "IN_DATA"));
            workflow.Arguments.Add(new Argument("name", "String", "CARRIERINFOSEND"));
            lstWorkflow.Add(workflow);

            workflow = new Workflow(modelID, fileName);
            workflow.DisplayName = "UpdateEismsgName";
            workflow.LibraryName = "DDEIS.Common";
            workflow.ClassName = "DDEIS.BaseActivityforB1";
            workflow.MethodName = "UpdateDDEISMessageName";
            workflow.Arguments.Add(new Argument("eisMsg", "Object", "IN_DATA"));
            workflow.Arguments.Add(new Argument("name", "String", "PROCESSCANCEL"));
            lstWorkflow.Add(workflow);

            workflow = new Workflow(modelID, fileName);
            workflow.DisplayName = "DynamicInvokeActivity";
            workflow.LibraryName = "DDEIS.B2.Activity";
            workflow.ClassName = "DDEIS.HSMSActivity_DDEIS";
            workflow.MethodName = "Send_S2F41_HostCommandSend";
            workflow.Arguments.Add(new Argument("msg", "Object", "obj"));
            lstWorkflow.Add(workflow);

            return lstWorkflow.ToArray();
        }


    }

}
