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


namespace DataProcessing
{
    public partial class FormOctree : Form
    {
        public FormMain mainForm = null;
        
        public FormOctree()
        {
            InitializeComponent();
        }       

        public void Enable()
        {
            panelOctree.Enabled = true;
            panelOctreeButtons.Enabled = true; 
        }

        public void Disable()
        {
            panelOctree.Enabled = false;
            panelOctreeButtons.Enabled = false;  
        }

        private void btnOctree_Click(object sender, EventArgs e)
        {
            if (mainForm.SelectedMesh != null)
            {
                if (!backgroundWorker1.IsBusy)
                {
                    mainForm.StartAction();
                    backgroundWorker1.RunWorkerAsync(comboBoxMethod.Text);
                }
                else
                    MessageBox.Show("Action is still in progress... please wait."); 
            }
        }

        private void btnDeleteOctree_Click(object sender, EventArgs e)
        {
            if (mainForm.SelectedMesh != null)
            {
                mainForm.SaveState();
                mainForm.SelectedMesh.DeleteSegments();                
                mainForm.ReDraw();
                mainForm.UpdateValues();
            }
        }

        private void FormOctree_FormClosed(object sender, FormClosedEventArgs e)
        {
            mainForm.OctreeForm = null;
        }

        //backgroundworker provadi vypocet napozadi (v jinem threadu)
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            string method = e.Argument as string;

            e.Result = mainForm.SelectedMesh.MakeOctree((int)numBoxParameter.Value, method, (int)numBoxMinPoints.Value, chckBoxMerge.Checked);        
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {          
            mainForm.StopAction();
            mainForm.ReDraw();
        }

        private void comboBoxMethod_TextChanged(object sender, EventArgs e)
        {
            if (comboBoxMethod.Text == "Depth")
            {
                labelParameter.Text = "Octree Depth:";
            }
            else
            {
                labelParameter.Text = "Min. Node Size:";
            }
        }
        
        ToolTip toolTipOctree = new ToolTip();

        private void FormOctree_Load(object sender, EventArgs e)
        {
            toolTipOctree.SetToolTip(this.labelMethod, "Volba metody pro tvorbu stromu.\r\nDepth - zadává se požadovaná hloubka stromu\r\nNode Size - zadává se maximální velikost generovaného segmentu");
            toolTipOctree.SetToolTip(this.labelParameter, "Hodnota parametru podle zvolené metody.\r\nMaximální velikost generovaného segmentu je zadávána v cm.");
            toolTipOctree.SetToolTip(this.chckBoxMerge, "Pokud rodičovský uzel segmentu expandoval všech 8 potomků\r\nbude jako segment generován pouze rodičovský uzel.");
            toolTipOctree.SetToolTip(this.btnOctree , "Spustí segmentaci podle zadaných parametrů.");
            toolTipOctree.SetToolTip(this.btnDeleteOctree , "Smaže všechny segmenty.");
            toolTipOctree.SetToolTip(this.labelMinPoints , "Minimalni počet bodu,které má obsahovat segment.\r\nPokud jich bude obsahovat méně nebude generován.");
            
            toolTipOctree.ShowAlways = true;
            toolTipOctree.AutomaticDelay = 6000;
            toolTipOctree.InitialDelay = 500;
        }

        //nutne kvuli bugu na winXP kdy se tooltip objevi pouze 1x
        private void labelMethod_MouseEnter(object sender, EventArgs e)
        {
            toolTipOctree.Active = false;
            toolTipOctree.Active = true;
        }

        private void labelParameter_MouseEnter(object sender, EventArgs e)
        {
            toolTipOctree.Active = false;
            toolTipOctree.Active = true;
        }

        private void chckBoxMerge_MouseEnter(object sender, EventArgs e)
        {
            toolTipOctree.Active = false;
            toolTipOctree.Active = true;
        }

        private void btnOctree_MouseEnter(object sender, EventArgs e)
        {
            toolTipOctree.Active = false;
            toolTipOctree.Active = true;
        }

        private void btnDeleteOctree_MouseEnter(object sender, EventArgs e)
        {
            toolTipOctree.Active = false;
            toolTipOctree.Active = true;
        }

        private void labelMinPoints_MouseEnter(object sender, EventArgs e)
        {
            toolTipOctree.Active = false;
            toolTipOctree.Active = true;
        }
    }
}