using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
namespace BulkTap3
{
    public partial class InsertFrm : Form
    {
        List<functions.Changes> inserted_list = new List<functions.Changes>();
        List<functions.Changes> main_inserted_list;
        TreeView MainTreeview;
        public InsertFrm()
        {
            InitializeComponent();
        }

        private void Insert_Load(object sender, EventArgs e)
        {
           
        }

        public void get_missing_identifier(TreeNode Parent_indentifier, TreeView tree, List<functions.Changes> Inset_List, string Filename)
        {

            functions.Changes inserted_iden = new functions.Changes();


            insert_iden_list.Items.Clear();
            string resource_data = Properties.Resources.TAP312;
            var filename = "TAP312.txt";
            System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("BulkTap3." + filename);

            // var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("BulkTap3.Tap312.txt"));
            int P_level=-1;
            int P_line=-1;

            string fileContent = Properties.Resources.TAP312;
            using (var reader = new StringReader(fileContent))
            {
                string line;
                int line_num=0;
                bool found = false;
                List<int> keys = new List<int>();

                List<char> Parent = new List<char>();
                while ((line = reader.ReadLine()) != null)
                {
                    
                    //3,OperatorSpecInformation,,163,ASCII,C,,
                    
                    string[] split = line.Split(',');
                    string Level = split[0];
                    string iden_name = split[1];
                    string iden_lbl = split[2];
                    string key = split[3];
                    string Type = split[4];
                    string PC = split[5];
                    string from = split[6];
                    string type = split[7];

                    if (Level == P_level.ToString() && found)
                    {
                        break;
                    }

                    if (iden_name == Parent_indentifier.Text)
                    {
                        P_level =int.Parse( Level);
                        P_line = line_num;
                        found = true;
                    }
                    if (Level == (P_level + 1).ToString() && found)
                    {
                        insert_iden_list.Items.Add(iden_name);
                        keys.Add(int.Parse(key));
                        Parent.Add(PC[0]);
                    }
                   
                    line_num++;
                    
                }

                
                bool NOT_found = true;
                string[] str1 = Parent_indentifier.Tag.ToString().Split(',');
                
                int node_index = 0;  // node index of node before the new identifer
                

                for (int count = 0; count < insert_iden_list.Items.Count; count++) 
                        {
                          
                            NOT_found = true;
                            foreach (TreeNode node in Parent_indentifier.Nodes)   
                            {
                                 
                        string[] str = node.Text.Split(':');
                        if (str[0] == insert_iden_list.Items[count].ToString())
                        {
                            string Node_tag = node.Tag.ToString();
                            str1 = Node_tag.Split(',');
                            node_index = node.Index+1;
                            
                            NOT_found = false;
                            break;
                            
                        }
                       
                       
                    }
                        if (NOT_found)
                        {
                            if (node_index > 0 || str1[1] == str1[2])
                            {
                                inserted_iden.identifer_start = int.Parse(str1[2]);
                                inserted_iden.identifer_end = int.Parse(str1[2]);
                            }
                            else
                            {
                                byte[] array = File.ReadAllBytes(Filename);
                                int index=functions.valueINDEX_readindntifier(array, array.Length, int.Parse(str1[1]));// getting the value INDEX
                                inserted_iden.identifer_start = index;
                                inserted_iden.identifer_end = index;
                            }
                            inserted_iden.Node_index=node_index;
                            inserted_iden.Parent_Name =  int.Parse( Parent_indentifier.Tag.ToString().Split(',')[0]);
                            //inserted_iden.identifer = int.Parse(str1[0]);
                            //inserted_iden.identifer_name = insert_iden_list.Items[count].ToString();
                            inserted_iden.identifer = keys[count];
                            if (Parent[count] == 'P')
                                inserted_iden.Parent = true;
                            else
                                inserted_iden.Parent = false;
                            inserted_iden.type = 2;
                            inserted_list.Add(inserted_iden);
                            
                        }
                    
                }
                foreach (TreeNode node in Parent_indentifier.Nodes)
                {
                    for (int count=0;count<insert_iden_list.Items.Count;count++)
                    {
                        string[] str = node.Text.Split(':');
                        if (str[0]==insert_iden_list.Items[count].ToString())
                        {
                            insert_iden_list.Items.RemoveAt(count);
                            
                            //inserted_list.RemoveAt(count);
                        }
                    }

                }
                MainTreeview = tree;
                main_inserted_list = Inset_List;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (insert_iden_list.SelectedIndex >= 0)
            {
                string dont = "";
                byte tree_imageindex = 0;
                if (inserted_list[insert_iden_list.SelectedIndex].Parent)
                {
                    dont = "";
                    tree_imageindex = 0;
                }
                else
                {
                    dont = ":        ";
                    tree_imageindex = 6;
                }
                if (inserted_list[insert_iden_list.SelectedIndex].Node_index == MainTreeview.SelectedNode.GetNodeCount(false) || MainTreeview.SelectedNode.GetNodeCount(false) == 0)
                {

                    MainTreeview.SelectedNode.Nodes.Add(insert_iden_list.SelectedItem.ToString(), insert_iden_list.SelectedItem.ToString() + dont, tree_imageindex, tree_imageindex);
                    MainTreeview.SelectedNode.Nodes[MainTreeview.SelectedNode.GetNodeCount(false) - 1].Tag = inserted_list[insert_iden_list.SelectedIndex].identifer + "," + inserted_list[insert_iden_list.SelectedIndex].identifer_start + "," + inserted_list[insert_iden_list.SelectedIndex].identifer_start;
                    main_inserted_list.Add(inserted_list[insert_iden_list.SelectedIndex]);

                }
                else
                {
                    MainTreeview.SelectedNode.Nodes.Insert(inserted_list[insert_iden_list.SelectedIndex].Node_index, insert_iden_list.SelectedItem.ToString(), insert_iden_list.SelectedItem.ToString() + dont, tree_imageindex, tree_imageindex);
                    MainTreeview.SelectedNode.Nodes[inserted_list[insert_iden_list.SelectedIndex].Node_index].Tag = inserted_list[insert_iden_list.SelectedIndex].identifer + "," + inserted_list[insert_iden_list.SelectedIndex].identifer_start + "," + inserted_list[insert_iden_list.SelectedIndex].identifer_start;
                    main_inserted_list.Add(inserted_list[insert_iden_list.SelectedIndex]);

                }
                inserted_list.Clear();
                this.Hide();
            }
            //mainfrm.Insert_missing_identifer(inserted_list[insert_iden_list.SelectedIndex].Node_index, insert_iden_list.SelectedItem.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
