using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace BulkTap3
{
    public partial class Insert_Seq : Form
    {
        List<functions.Changes> inserted_list = new List<functions.Changes>();
        List<functions.Changes> main_inserted_list;
        functions.Changes inserted_iden = new functions.Changes();
        TreeView MainTreeview;
        TreeNode Parent_node;
        string main_Filename;
        string child;
        int child_identifier;
        int child_satrt;
        
        public Insert_Seq()
        {
            InitializeComponent();
        }

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

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



            string fileContent = Properties.Resources.Indentifier_seq;
            using (var reader = new StringReader(fileContent))
            {
                string line;

                while ((line = reader.ReadLine()) != null)
                {

                    //MessageDescriptionInfoList,8,MessageDescriptionInformation

                    string[] split = line.Split(',');
                    string Parent_string = split[0];
                    string Parent_Iden = split[1];
                    string Child_string = split[2];
                    string Child_Iden = split[3];

                    string[] str1 = Parent_indentifier.Tag.ToString().Split(',');

                    if (Parent_Iden == str1[0])
                    {
                        child = Child_string;
                        child_identifier = int.Parse(Child_Iden);
                        

                        inserted_iden.Parent_Name = int.Parse(Parent_Iden);
                        break;
                    }
                }
                

                Label1.Text = "Parent Contain ( " + Parent_indentifier.GetNodeCount(false).ToString() + " ) of" + child;
                insert_index.Maximum = Parent_indentifier.GetNodeCount(false) + 1;
                MainTreeview = tree;
                main_inserted_list = Inset_List;
                Parent_node = Parent_indentifier;

                inserted_iden.Parent = true;
                inserted_iden.identifer = child_identifier;
                
                inserted_iden.type = 2;
                main_Filename = Filename;
            }
        }
        private void Insert_Seq_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            //|| MainTreeview.SelectedNode.GetNodeCount(false) == 0
            if (insert_index.Value == MainTreeview.SelectedNode.GetNodeCount(false)+1 )
            {
                string[] str = Parent_node.Nodes[MainTreeview.SelectedNode.GetNodeCount(false) - 1].Tag.ToString().Split(','); //get the last Identifer end
                MainTreeview.SelectedNode.Nodes.Add(child, child, 0, 0);
                MainTreeview.SelectedNode.Nodes[MainTreeview.SelectedNode.GetNodeCount(false) - 1].Tag = child_identifier + "," + str[2] + "," + str[2];
                inserted_iden.identifer_start = int.Parse(str[2]);
                inserted_iden.identifer_end = int.Parse(str[2]);
                inserted_iden.Node_index = (int)insert_index.Value - 1;
                main_inserted_list.Add(inserted_iden);

            }
            else if (insert_index.Value > 1)
            {
                string[] str = Parent_node.Nodes[(int)insert_index.Value -2].Tag.ToString().Split(','); 
                
                MainTreeview.SelectedNode.Nodes.Insert((int)insert_index.Value -1, child, child, 0, 0);
               
                
                    inserted_iden.identifer_start = int.Parse(str[2]);
                    inserted_iden.identifer_end = int.Parse(str[2]);
                    MainTreeview.SelectedNode.Nodes[(int)insert_index.Value - 1].Tag = child_identifier + "," + str[2] + "," + str[2];
               
                inserted_iden.Node_index = (int)insert_index.Value - 2;
                main_inserted_list.Add(inserted_iden);

            }
            else if (insert_index.Value == 1)
            {
                string[] str = Parent_node.Tag.ToString().Split(',');
                MainTreeview.SelectedNode.Nodes.Insert((int)insert_index.Value - 1, child, child, 0, 0);
                
                    byte[] array = File.ReadAllBytes(main_Filename);
                    int index = functions.valueINDEX_readindntifier(array, array.Length, int.Parse(str[1]));// getting the value INDEX
                    inserted_iden.identifer_start = index;
                    inserted_iden.identifer_end = index;
                    MainTreeview.SelectedNode.Nodes[(int)insert_index.Value - 1].Tag = child_identifier + "," + index + "," + index;

                    inserted_iden.Node_index = (int)insert_index.Value - 1;
                    main_inserted_list.Add(inserted_iden);
            }
            this.Hide();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
