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;

namespace BulkTap3
{
    public partial class Find : Form
    {
        TreeView main_tree;
        TreeView branch_tree;
        string filename;
        List<functions.Changes> Changes;
        public Find()
        {
            InitializeComponent();
        }
        public void TreeNode(TreeView maintree, TreeView branchtree, string Opened_filename, List<functions.Changes> ChangeList)
        {
            main_tree = maintree;
            branch_tree = branchtree;
            filename = Opened_filename;
            Changes = ChangeList;
        }
        private void Find_Load(object sender, EventArgs e)
        {

        }
        List<TreeNode> Found_nodes=new List<TreeNode>();
        int next = 0;
        private void Findbtn_Click(object sender, EventArgs e)
        {
            if (Findbtn.Text=="Find")
            {
                if (child_Iden_cb.Text != "" && find_txt.Text != "")
                {
                    int indentifier = 0;
                    for (int Count = 0; Count < functions.indentefiers_Tree.Length; Count++)
                    {
                        if (child_Iden_cb.Text == functions.indentefiers_Tree[Count].cidentifier)
                        {
                            indentifier = functions.indentefiers_Tree[Count].ckey;

                            break;
                        }
                    }
                    if (indentifier != 0)
                    {
                        TreeNode[] Found = functions.Find_by_identifier(main_tree.Nodes[0].Nodes[3], indentifier, find_txt.Text, filename, Changes);
                        Found_nodes.AddRange(Found); // convert to list 

                        if (Found_nodes.Count > 0)
                        {
                            
                            main_tree.SelectedNode = Found_nodes[0];
                            main_tree.Focus();
                            branch_tree.SelectedNode = functions.FindNameInTreeView(branch_tree.Nodes[0], child_Iden_cb.Text + ":        " + find_txt.Text);
                             
                            Findbtn.Text = "Find next";
                            replacebtn.Enabled = true;
                            replaceallbtn.Enabled = true;
                            removealltbn.Enabled = true;
                            removebtn.Enabled = true;
                            next++;
                            return;
                            //   Filtertree.Nodes[0].Nodes[3].Nodes.Clear();
                            //  Filtertree.Nodes[0].Nodes[3].Nodes.AddRange(filtered_node);
                        }
                        else
                        {
                            MessageBox.Show("Cannot find " + child_Iden_cb.Text + " : " + find_txt.Text  );
                        }
                    }
                }
            }

            if (Findbtn.Text == "Find next")
            {
            first:
                if (next <= Found_nodes.Count - 1)
                {

                    main_tree.SelectedNode = Found_nodes[next];
                    main_tree.Focus();
                    branch_tree.SelectedNode = functions.FindNameInTreeView(branch_tree.Nodes[0], child_Iden_cb.Text + ":        " + find_txt.Text);
                    branch_tree.Focus();
                    next++;
                }
                else if (Found_nodes.Count == 0)
                {
                    reset();
                    return;
                }
                else
                {
                    next = 0;
                    goto first;
                }
            }
        }

        private void replacebtn_Click(object sender, EventArgs e)
        {
            main_tree.SelectedNode = Found_nodes[next-1];
            main_tree.Focus();
            branch_tree.SelectedNode = functions.FindNameInTreeView(branch_tree.Nodes[0], child_Iden_cb.Text + ":        " + find_txt.Text);
            branch_tree.Focus();
            if (branch_tree.SelectedNode != null)
            {
                string[] nodename = branch_tree.SelectedNode.Text.Split(':');
                branch_tree.SelectedNode.Text = nodename[0] + ":        " + replace_txt.Text;
                functions.Changes New_Edit;
                string Node_tag = branch_tree.SelectedNode.Tag.ToString();
                string[] str = Node_tag.Split(',');
                New_Edit.identifer = int.Parse(str[0]);
                New_Edit.identifer_start = int.Parse(str[1]);
                New_Edit.identifer_end = int.Parse(str[2]);
                New_Edit.type = 0;
                New_Edit.Parent = false;
                New_Edit.Node_index = branch_tree.SelectedNode.Index;
                New_Edit.Parent_Name = int.Parse(branch_tree.SelectedNode.Parent.Tag.ToString().Split(',')[0]); ;
                New_Edit.New_Value = replace_txt.Text;
                if (replace_txt.Text != nodename[1].TrimStart())
                    Changes.Add(New_Edit);
                Found_nodes.RemoveAt(next - 1);
                next--;

                if (next < Found_nodes.Count - 1)
                {

                    main_tree.SelectedNode = Found_nodes[next];
                    main_tree.Focus();
                    branch_tree.SelectedNode = functions.FindNameInTreeView(branch_tree.Nodes[0], child_Iden_cb.Text + ":        " + find_txt.Text);
                    branch_tree.Focus();
                    next++;
                }
                else
                {
                    MessageBox.Show("All Founded Values have been removed");
                }
                if (Found_nodes.Count == 0)
                {
                    reset();

                }
            }
        }

        private void replaceallbtn_Click(object sender, EventArgs e)
        {
            DialogResult user_answer ;
            if (Found_nodes.Count > 20)
            {
                user_answer = MessageBox.Show(this, "there is " + Found_nodes.Count + "founds, it will be time consuming \n\r use the \n\r Do you want to continue?", "Worning", MessageBoxButtons.YesNo);
            }
            else
                user_answer = DialogResult.Yes;
            if (user_answer == DialogResult.Yes)
            {
                for (next = 0; next < Found_nodes.Count; next++)
                {
                    main_tree.SelectedNode = Found_nodes[next];
                    main_tree.Focus();
                    branch_tree.SelectedNode = functions.FindNameInTreeView(branch_tree.Nodes[0], child_Iden_cb.Text + ":        " + find_txt.Text);
                    branch_tree.Focus();
                    string[] nodename = branch_tree.SelectedNode.Text.Split(':');
                    branch_tree.SelectedNode.Text = nodename[0] + ":        " + replace_txt.Text;
                    functions.Changes New_Edit;
                    string Node_tag = branch_tree.SelectedNode.Tag.ToString();
                    string[] str = Node_tag.Split(',');
                    New_Edit.identifer = int.Parse(str[0]);
                    New_Edit.identifer_start = int.Parse(str[1]);
                    New_Edit.identifer_end = int.Parse(str[2]);
                    New_Edit.type = 0;
                    New_Edit.Parent = false;
                    New_Edit.Node_index = branch_tree.SelectedNode.Index;
                    New_Edit.Parent_Name = int.Parse(branch_tree.SelectedNode.Parent.Tag.ToString().Split(',')[0]); ;
                    New_Edit.New_Value = replace_txt.Text;
                    if (replace_txt.Text != nodename[1].TrimStart())
                        Changes.Add(New_Edit);


                }
            }
            Found_nodes.Clear();
            reset();
        }

        private void reset()
        {
            Findbtn.Text = "Find";
            replacebtn.Enabled = false;
            replaceallbtn.Enabled = false;
            removealltbn.Enabled = false;
            removebtn.Enabled = false;
            next = 0;
        }
        private void removebtn_Click(object sender, EventArgs e)
        {
            if (next < Found_nodes.Count)
            {
                main_tree.SelectedNode = Found_nodes[next-1];
                main_tree.Focus();
                branch_tree.SelectedNode = functions.FindNameInTreeView(branch_tree.Nodes[0], child_Iden_cb.Text + ":        " + find_txt.Text);
                branch_tree.Focus();
            }
            if (branch_tree.SelectedNode != null)
            {
                functions.Changes Deleted_Item = new functions.Changes();


                string[] str = branch_tree.SelectedNode.Tag.ToString().Split(','); ;
                Deleted_Item.identifer = int.Parse(str[0]);
                Deleted_Item.identifer_start = int.Parse(str[1]);
                Deleted_Item.identifer_end = int.Parse(str[2]);
                Deleted_Item.Node_index = branch_tree.SelectedNode.Index;
                Deleted_Item.type = 1;
                Deleted_Item.Parent = (branch_tree.SelectedNode.GetNodeCount(false) > 0) ? true : false;
                Deleted_Item.Parent_Name = int.Parse(branch_tree.SelectedNode.Parent.Tag.ToString().Split(',')[0]);
                Changes.Add(Deleted_Item);
                branch_tree.SelectedNode.Remove();
                Found_nodes.RemoveAt(next - 1);
                next--;

                if (next <= Found_nodes.Count - 1)
                {

                    main_tree.SelectedNode = Found_nodes[next];
                    main_tree.Focus();
                    branch_tree.SelectedNode = functions.FindNameInTreeView(branch_tree.Nodes[0], child_Iden_cb.Text + ":        " + find_txt.Text);
                    branch_tree.Focus();
                    next++;
                }
                else
                {
                    MessageBox.Show("All Founded Values have been removed");
                }
                if (Found_nodes.Count == 0)
                {
                    reset();

                }
            }
        }

        private void removealltbn_Click(object sender, EventArgs e)
        {
            for (next = 0; next < Found_nodes.Count; next++)
            {
                main_tree.SelectedNode = Found_nodes[next];
                main_tree.Focus();
                branch_tree.SelectedNode = functions.FindNameInTreeView(branch_tree.Nodes[0], child_Iden_cb.Text + ":        " + find_txt.Text);
                branch_tree.Focus();


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


                string[] str = branch_tree.SelectedNode.Tag.ToString().Split(','); ;
                Deleted_Item.identifer = int.Parse(str[0]);
                Deleted_Item.identifer_start = int.Parse(str[1]);
                Deleted_Item.identifer_end = int.Parse(str[2]);
                Deleted_Item.Node_index = branch_tree.SelectedNode.Index;
                Deleted_Item.type = 1;
                Deleted_Item.Parent = (branch_tree.SelectedNode.GetNodeCount(false) > 0) ? true : false;
                Deleted_Item.Parent_Name = int.Parse(branch_tree.SelectedNode.Parent.Tag.ToString().Split(',')[0]);
                Changes.Add(Deleted_Item);
                branch_tree.SelectedNode.Remove();

            }
            Found_nodes.Clear();
            reset();
        }

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