using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Xml.Serialization;
using MyGeometry;


namespace FunctionalSubstructure
{
    public partial class FormMain : Form
    {
        // constructor
        public FormMain()
        {
            InitializeComponent();

			

        }
		

        // event handlers
		private void FormMain_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
		}
		private void FormMain_KeyUp(object sender, KeyEventArgs e)
		{
			switch (e.KeyData)
			{
				case Keys.F1:
					break;
				case Keys.F2:
					
					break;
				case Keys.F3:
					
					break;
				case Keys.F4:
					
					break;
				case Keys.F5:
					
					break;
				case Keys.F6:
					
					break;
				case Keys.F7:
					
					break;
				case Keys.F8:
					
					break;
				case Keys.F9:
					
					break;
                case Keys.F10:
                    
                    break;
                
			}
		}
        private void FormMain_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.D:
                    
                    break;
            }
        }

		
		private void buttonCloseModel_Click(object sender, EventArgs e)
		{
			this.meshView.Clear();

			if (this.tabControlMain.SelectedTab == null) return;

		}
		private void buttonShowHideRightPanel_Click(object sender, EventArgs e)
        {
            this.splitContainer1.Panel2Collapsed =
                !this.splitContainer1.Panel2Collapsed;
        }
		private void buttonShowHideDownPanel_Click(object sender, EventArgs e)
		{
			this.splitContainer2.Panel2Collapsed =
				!this.splitContainer2.Panel2Collapsed;
		}
		private void buttonClearOutputText_Click(object sender, EventArgs e)
		{
			this.textBoxOutputText.Text = "";
		}
		private void toolStripButtonShowHideEdges_Click(object sender, EventArgs e)
		{
			meshView.showWireFrame = !meshView.showWireFrame;
			toolStripButtonShowHideEdges.Checked = meshView.showWireFrame;
			meshView.Refresh();
		}
		private void toolStripButton_3Synthesize_Click(object sender, EventArgs e)
		{
			this.meshView.Clear();

			int _count = 0;
			if (this._shape_triplet != null)
			{
				foreach (ComponentGraph g in this._shape_triplet)
				{
					if (g != null)
					{
						_count++;
					}
				}
			}

			if (_count < 2)
			{
				Program.OutputText("please select at least 2 objects!", true);
				return;
			}

			int K = this._shape_triplet.Length;

			Program.OutputText("--------------------------------------------", true);
			for (int i = 0; i < K; ++i)
			{
				ComponentGraph srcgraph = this._shape_triplet[i];
				if (srcgraph == null) continue;

				List<ComponentGraph> lsg = new List<ComponentGraph>();
				for (int j = 0; j < K; ++j)
				{
					if (j == i) continue;
					ComponentGraph g = this._shape_triplet[j];
					if (g != null)
						lsg.Add(g);
				}

				this.meshView.TripletsGraphReshuffle(srcgraph, lsg);
				this.meshView.Refresh();
			}
			Program.OutputText("# mesh generated: " + this.meshView.synthesizedCuboidMeshComponents.Count, true);
			Program.OutputText("--------------------------------------------", true);

			this._shape_triplet = null;
			this._curr_shape_index = 0;

		}
		private void toolStripButtonPreviousModel_Click(object sender, EventArgs e)
		{
			this.meshView.PrevModel();
		}
		private void toolStripButtonNxtModel_Click(object sender, EventArgs e)
		{
			this.meshView.NextModel();
		}


		private void FormMain_Load(object sender, EventArgs e)
		{
			string dir = @".\model_images\";

			string[] files = Directory.GetFiles(dir, "*.png");

			List<Image> thumbs = new List<Image>();
			int index = 0;
			foreach (string file in files)
			{
				Bitmap img = new Bitmap(file);
				thumbs.Add(img);

				int s = file.LastIndexOf('\\');
				int t = file.LastIndexOf('.');
				string name = file.Substring(s + 1, t - s - 1);
				foreach (ComponentGraph g in MeshView.spatialgraphs)
				{
					string _file = g.meshfile;
					int k = _file.LastIndexOf('\\');
					string objname = _file.Substring(k + 1, _file.Length - k - 5);
					if (objname.ToLower() == name.ToLower())
					{
						_all_shapes.Add(g);
						break;
					}
				}

				index++;
			}

			this.MakeThumnails(thumbs);

			foreach (ComponentGraph g in _all_shapes)
			{
				g.GetExtraSymmetryTriplets();
			}
		}



		// public functions
		private ComponentGraph[] _shape_triplet = null;
		private List<ComponentGraph> _all_shapes = new List<ComponentGraph>();
		private int _curr_shape_index = 0;


		public void OutputText(string s, bool newLine)
		{
			if (newLine)
				this.textBoxOutputText.AppendText(s + System.Environment.NewLine);
			else
				this.textBoxOutputText.AppendText(s);
		}
		public void MakeThumnails(List<Image> images)
		{
			this.flpThumbnails.StopThumbnailsGeneration();
			this.flpThumbnails.ShowThumbnails(images);
		}
		public void RegisterObject(int thumbIndex)
		{
			if (this._all_shapes == null || thumbIndex < 0 || thumbIndex >= this._all_shapes.Count) return;
			if (this._shape_triplet == null)
			{
				this._shape_triplet = new ComponentGraph[3];
				_curr_shape_index = 0;
			}
			ComponentGraph _shape = this._all_shapes[thumbIndex];
			for (int i = 0; i < 3; ++i)
			{
				if (this._shape_triplet[i] == _shape)
				{
					Program.OutputText("object already in list ...", true);
					return;
				}
			}
			Program.OutputText(" -- selected object, index = " + _curr_shape_index, true);
			this._shape_triplet[_curr_shape_index] = _shape;
			_curr_shape_index = (_curr_shape_index + 1) % 3;
		}


		// NEW_ADD
		public MeshView GetMeshView()
		{
			return meshView;
		}
    }
}
