using Remontor.Connector;
using Remontor.History;
using Remontor.Setting;
using Remontor.Words;
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 static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Remontor
{
    internal partial class FlowContainerWords : UserControl
    {
        private bool Showed = true;
        private Size NormalSize;
        private TreeNode SelectedNode;
        private bool tapNode;
        public FlowContainerWords()
        {
            InitializeComponent();
            NormalSize = this.Size;
            


            //WordsList.MainCategory = SGlobalSetting.LoadWords();

            WordsTree.Nodes.AddRange(WordsList.ListNodes());
            SGlobalSetting.settingExpand.ExpendAll(WordsTree.Nodes);

        }

        public void UpdateTree()
        {
            WordsTree.Nodes.Clear();
            WordsTree.Nodes.AddRange(WordsList.ListNodes());
            SGlobalSetting.settingExpand.ExpendAll(WordsTree.Nodes);

        }

        public void Save()
        {
            SGlobalSetting.settingExpand.SaveExpand(WordsTree.Nodes);
        }

        public bool HideBar(BarShow barShow)
        {
            if(barShow == BarShow.ShowWords)
            {
                //this.Height = NormalSize.Height;
                this.Visible = true;
            }
            else
            {
               // this.Height = 0;
                this.Visible = false;
            }
            
            return this.Visible;
        }


        public void UpdateHistory(List<HistoryItem> items)
        {
            
            
        }
        private void CloseMenu()
        {
            tapNode = false;


        }


        private void TreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {

            System.Windows.Forms.TreeView tree = (System.Windows.Forms.TreeView)sender;

            Rectangle BoundsNode = new Rectangle(e.Node.Bounds.X - 43, e.Node.Bounds.Y, e.Node.Bounds.Width + 43, e.Node.Bounds.Height);
            if (e.Button == MouseButtons.Right)
            {
                if (BoundsNode.Contains(e.Location) && e.Node.Tag is WordsComp)
                {
                    
                    Control control = (Control)sender;
                    SConnector.FormMain.MenuStripCommand.ShowMenuWords(((WordsComp)e.Node.Tag).GetComp(), control.PointToScreen(e.Location), CloseMenu);
                    tree.SelectedNode = e.Node;
                    tapNode = true;
                }
                else
                {
                    tree.SelectedNode = null;
                }

            }
            if (e.Button == MouseButtons.Left)
            {
                if (BoundsNode.Contains(e.Location) && e.Node.Tag is WordsComp)
                {
                    tapNode = true;
                    if (e.Node.IsSelected)
                    {
                        //SConnector.NewConnect(((WordsComp)e.Node.Tag).GetComp());
                        //tree.SelectedNode = null;
                        
                    }
                    else
                    {
                       // System.Console.WriteLine("click1");
                        SelectedNode = e.Node;
                       // tree.SelectedNode = e.Node;
                        
                  
                    }


                    // cm_words.Show(tree.PointToScreen(e.Location));
                }
                else
                {
                    tree.SelectedNode = null;
                }

            }
            //tree.BeginUpdate();

            //  for (int i = 0; i < tree.Nodes.Count; i++)
            //  {
            //      treeView1_treeViewChangeRootCheckBox(tree.Nodes[i]);
            //  }


        }

        private void WordsTree_MouseClick(object sender, MouseEventArgs e)
        {
            if (tapNode)
            {
                tapNode = false;
            }
            else
            {
                System.Windows.Forms.TreeView tree = (System.Windows.Forms.TreeView)sender;
                tree.SelectedNode = null;
            }
            

        }

        private void WordsTree_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //System.Console.WriteLine("double");
            if(SelectedNode != null) SConnector.NewConnect(((WordsComp)SelectedNode.Tag).GetComp());
            
            SelectedNode = null;
        }
    }
}
