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

namespace DataProcessing
{
    public partial class FormEditation : Form
    {
        public FormMain mainForm = null;
        ToolTip toolTipEditation = new ToolTip();
        
        public FormEditation()
        {
            InitializeComponent();
        }

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

        public void Enable()
        {
            panelPosition.Enabled = true;
            panelRotation.Enabled = true;
            panelEditation.Enabled = true;
        }

        public void Disable()
        {
            panelPosition.Enabled = false;
            panelRotation.Enabled = false;
            panelEditation.Enabled = false;
        }

        public void UpdateValues()
        {
            if (mainForm.SelectedMesh != null)
            {
                numBoxPositionX.Value = (decimal)mainForm.SelectedMesh.PositionX;
                numBoxPositionY.Value = (decimal)mainForm.SelectedMesh.PositionY;
                numBoxPositionZ.Value = (decimal)mainForm.SelectedMesh.PositionZ;

                numBoxRotationX.Value = (decimal)mainForm.SelectedMesh.CurrentRotationX;
                numBoxRotationY.Value = (decimal)mainForm.SelectedMesh.CurrentRotationY;
                numBoxRotationZ.Value = (decimal)mainForm.SelectedMesh.CurrentRotationZ;
            }
            else
            {
                numBoxPositionX.Value = 0;
                numBoxPositionY.Value = 0;
                numBoxPositionZ.Value = 0;

                numBoxRotationX.Value = 0;
                numBoxRotationY.Value = 0;
                numBoxRotationZ.Value = 0;
            }
        }

        private void numBoxPositionX_ValueChanged(object sender, EventArgs e)
        {
            if (mainForm.SelectedMesh != null)
            {
                mainForm.SelectedMesh.PositionX = (float)numBoxPositionX.Value;
                mainForm.ReDraw();
            }
        }

        private void numBoxPositionY_ValueChanged(object sender, EventArgs e)
        {
            if (mainForm.SelectedMesh != null)
            {
                mainForm.SelectedMesh.PositionY = (float)numBoxPositionY.Value;
                mainForm.ReDraw();
            }
        }

        private void numBoxPositionZ_ValueChanged(object sender, EventArgs e)
        {
            if (mainForm.SelectedMesh != null)
            {
                mainForm.SelectedMesh.PositionZ = (float)numBoxPositionZ.Value;
                mainForm.ReDraw();
            }
        }

        private void numBoxRotationX_ValueChanged(object sender, EventArgs e)
        {
            if (mainForm.SelectedMesh != null)
            {
                if (mainForm.SelectedMesh.Segments.Count > 0 || mainForm.SelectedMesh.HoughVertices.Count > 0)
                {
                    if (MessageBox.Show("All detected elements will be lost on rotation. Continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        mainForm.SelectedMesh.DeleteReduction();
                        mainForm.SelectedMesh.DeleteLines();
                        mainForm.SelectedMesh.DeleteSurfaces();
                        mainForm.SelectedMesh.DeleteSegments();
                        mainForm.ReDraw();
                        mainForm.UpdateValues();
                    }
                    else
                    {
                        return;
                    }
                }

                mainForm.SelectedMesh.CurrentRotationX = (float)numBoxRotationX.Value;
                mainForm.ReDraw();
            }
        }

        private void numBoxRotationY_ValueChanged(object sender, EventArgs e)
        {
            if (mainForm.SelectedMesh != null)
            {
                if (mainForm.SelectedMesh.Segments.Count > 0 || mainForm.SelectedMesh.HoughVertices.Count > 0)
                {
                    if (MessageBox.Show("All detected elements will be lost on rotation. Continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        mainForm.SelectedMesh.DeleteReduction();
                        mainForm.SelectedMesh.DeleteLines();
                        mainForm.SelectedMesh.DeleteSurfaces();
                        mainForm.SelectedMesh.DeleteSegments();
                        mainForm.ReDraw();
                        mainForm.UpdateValues();
                    }
                    else
                    {
                        return;
                    }
                }

                mainForm.SelectedMesh.CurrentRotationY = (float)numBoxRotationY.Value;
                mainForm.ReDraw();
            }
        }

        private void numBoxRotationZ_ValueChanged(object sender, EventArgs e)
        {
            if (mainForm.SelectedMesh != null)
            {
                if (mainForm.SelectedMesh.Segments.Count > 0 || mainForm.SelectedMesh.HoughVertices.Count > 0)
                {
                    if (MessageBox.Show("All detected elements will be lost on rotation. Continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        mainForm.SelectedMesh.DeleteReduction();
                        mainForm.SelectedMesh.DeleteLines();
                        mainForm.SelectedMesh.DeleteSurfaces();
                        mainForm.SelectedMesh.DeleteSegments();
                        mainForm.ReDraw();
                        mainForm.UpdateValues();
                    }
                    else
                    {
                        return;
                    }
                }

                mainForm.SelectedMesh.CurrentRotationZ = (float)numBoxRotationZ.Value;
                mainForm.ReDraw();
            }
        }        

        private void btnAlign_Click(object sender, EventArgs e)
        {
            mainForm.SelectedMesh.AlignWithGrid();
            UpdateValues();
            mainForm.ReDraw();
        }   

        private void numBoxRotationX_Click(object sender, EventArgs e)
        {
            if (mainForm.SelectedMesh != null)
            {
                if (mainForm.SelectedMesh.Segments.Count > 0 || mainForm.SelectedMesh.HoughVertices.Count > 0)
                {
                    if (MessageBox.Show("All detected elements will be lost on rotation. Continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        mainForm.SelectedMesh.DeleteReduction();
                        mainForm.SelectedMesh.DeleteLines();
                        mainForm.SelectedMesh.DeleteSurfaces();
                        mainForm.SelectedMesh.DeleteSegments();
                        mainForm.ReDraw();
                        mainForm.UpdateValues();
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }

        private void numBoxRotationY_Click(object sender, EventArgs e)
        {
            if (mainForm.SelectedMesh != null)
            {
                if (mainForm.SelectedMesh.Segments.Count > 0 || mainForm.SelectedMesh.HoughVertices.Count > 0)
                {
                    if (MessageBox.Show("All detected elements will be lost on rotation. Continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        mainForm.SelectedMesh.DeleteReduction();
                        mainForm.SelectedMesh.DeleteLines();
                        mainForm.SelectedMesh.DeleteSurfaces();
                        mainForm.SelectedMesh.DeleteSegments();
                        mainForm.ReDraw();
                        mainForm.UpdateValues();
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }

        private void numBoxRotationZ_Click(object sender, EventArgs e)
        {
            if (mainForm.SelectedMesh != null)
            {
                if (mainForm.SelectedMesh.Segments.Count > 0 || mainForm.SelectedMesh.HoughVertices.Count > 0)
                {
                    if (MessageBox.Show("All detected elements will be lost on rotation. Continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        mainForm.SelectedMesh.DeleteReduction();
                        mainForm.SelectedMesh.DeleteLines();
                        mainForm.SelectedMesh.DeleteSurfaces();
                        mainForm.SelectedMesh.DeleteSegments();
                        mainForm.ReDraw();
                        mainForm.UpdateValues();
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }

        private void btnDeleteLines_Click(object sender, EventArgs e)
        {
            mainForm.SaveState();

            if (mainForm.SelectedMesh != null)
            {
                mainForm.SelectedMesh.DeleteLines();
                mainForm.ReDraw();
                UpdateValues();
            }           
        }

        private void btnDeleteSurfaces_Click(object sender, EventArgs e)
        {
            mainForm.SaveState();

            if (mainForm.SelectedMesh != null)
            {
                mainForm.SelectedMesh.DeleteSurfaces();
                mainForm.ReDraw();
                UpdateValues();
            }  
        }

        private void btnDeleteSegments_Click(object sender, EventArgs e)
        {
            mainForm.SaveState();

            if (mainForm.SelectedMesh != null)
            {
                mainForm.SelectedMesh.DeleteSegments();
                mainForm.ReDraw();
                mainForm.UpdateValues();
            }   
        }

        private void btnDeleteMesh_Click(object sender, EventArgs e)
        {
            mainForm.SaveState();

            if (mainForm.SelectedMesh != null)
            {
                mainForm.DeleteSelected();
                mainForm.ReDraw();
                mainForm.UpdateValues();
            }   
        }

        //tooltipy
        private void FormEditation_Load(object sender, EventArgs e)
        {
            toolTipEditation.SetToolTip(this.btnAlign, "Zarovná spodní stěnu objektu s mřížkou (na pozici 0.)");
            toolTipEditation.SetToolTip(this.btnDeleteLines, "Smaže všechny úsečky");
            toolTipEditation.SetToolTip(this.btnDeleteSurfaces, "Smaže všechny plochy");
            toolTipEditation.SetToolTip(this.btnDeleteSegments, "Smaže všechny segmenty");
            toolTipEditation.SetToolTip(this.btnDeleteMesh, "Smaže celý objekt");

            toolTipEditation.ShowAlways = true;
            toolTipEditation.AutomaticDelay = 6000;
            toolTipEditation.InitialDelay = 500;
        }

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

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

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

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

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