using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace friction
{
	public partial class MainForm : Form
	{
		string m_DBFileName = "";
		DataHandler m_DataHandler = new DataHandler();
		PanelMaterial m_MaterialPanel = null;
		PanelResult m_ResultPanel = null;
		PanelAnalysis m_AnalysisPanel = null;
		PanelCompatibility m_CompatibilityPanel = null;
		PanelRadarGraph m_RadarGraphPanel = null;
		PanelTrendGraph m_TrendGraphPanel = null;

		Report m_Report = null;

		public MainForm()
		{
			InitializeComponent();

			dockPanel.Theme = new VS2015DarkTheme();
			m_MaterialPanel = new PanelMaterial(this);
			m_ResultPanel = new PanelResult(this);
			m_AnalysisPanel = new PanelAnalysis(this);
			m_CompatibilityPanel = new PanelCompatibility(this);
			m_RadarGraphPanel = new PanelRadarGraph(this, m_DataHandler);
			m_TrendGraphPanel = new PanelTrendGraph(this, m_DataHandler);

			Theme.Apply(this);
			Theme.Apply(menuStrip);
			Theme.Apply(toolStripMain);
			Theme.Apply(statusStrip);

			Config.Init();
			UpdateRecentFile();
			if(Config.OPTION.WindowBound.Width > 0)
				Bounds = Config.OPTION.WindowBound;
		}

		void UpdateRecentFile()
		{
			recentToolStripMenuItem.DropDownItems.Clear();

			foreach (string file in Enumerable.Reverse(Config.OPTION.m_RecentList))
			{
				ToolStripItem item = recentToolStripMenuItem.DropDownItems.Add(file);
				item.BackColor = Theme.Backcolor;
				item.ForeColor = Theme.Forecolor;
				item.Click += RecentToolStripMenuItem_Click;
			}
		}

		private void RecentToolStripMenuItem_Click(object sender, EventArgs e)
		{
			string strFile = ((ToolStripItem)sender).Text;
			OpenDB(strFile);
		}

		private void OpenDB(string strFile=null)
		{
			if (strFile == null)
			{
				OpenFileDialog ofd = new OpenFileDialog();
				ofd.Filter = "엑셀 파일 (*.xlsx)|*.xlsx|엑셀 파일 (*.xls)|*.xls|전체|*";
				DialogResult result = ofd.ShowDialog();
				if (result == DialogResult.OK)
					m_DBFileName = ofd.FileName;
				else
					return;
			}
			else
			{
				m_DBFileName = strFile;
			}

			Cursor.Current = Cursors.WaitCursor;

			m_DataHandler.LoadData(m_DBFileName);

			toolStripButtonTrendGraph.Enabled = false;
			toolStripButtonRadarGraph.Enabled = true;

			m_MaterialPanel.UpdateData(m_DataHandler);
			OpenPanel(m_MaterialPanel);

			m_ResultPanel.UpdateData(m_DataHandler);
			OpenPanel(m_ResultPanel);

			m_AnalysisPanel.Reset();
			m_AnalysisPanel.UpdateData(m_DataHandler);
			OpenPanel(m_AnalysisPanel);

			toolStripStatusLabel.Text = m_DBFileName;

			Config.OPTION.AddRecentFile(m_DBFileName);
			UpdateRecentFile();

			Cursor.Current = Cursors.Default;
		}

		public void OpenPanel(DockContent panel)
		{
			if (panel.Visible == false)
				panel.Show(dockPanel);
			if (panel.IsHidden == true)
				panel.IsHidden = false;

			if(panel.DockState == DockState.DockTopAutoHide ||
				panel.DockState == DockState.DockBottomAutoHide ||
				panel.DockState == DockState.DockLeftAutoHide ||
				panel.DockState == DockState.DockRightAutoHide)
			{
				dockPanel.ActiveAutoHideContent = panel;
			}
		}

		#region Events from menu
		private void openDBToolStripMenuItem_Click(object sender, EventArgs e)
		{
			OpenDB();
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void resultTableToolStripMenuItem_Click(object sender, EventArgs e)
		{
			OpenPanel(m_ResultPanel);
		}

		private void analysisTableToolStripMenuItem_Click(object sender, EventArgs e)
		{
			OpenPanel(m_AnalysisPanel);
		}

		private void materialCompatibilityMapToolStripMenuItem_Click(object sender, EventArgs e)
		{
			OpenPanel(m_CompatibilityPanel);
			m_CompatibilityPanel.UpdateData(m_DataHandler);
		}

		private void radarGraphToolStripMenuItem_Click(object sender, EventArgs e)
		{
			OpenPanel(m_RadarGraphPanel);
			m_RadarGraphPanel.UpdateGraph();
		}

		private void trendGraphToolStripMenuItem_Click(object sender, EventArgs e)
		{
			OpenPanel(m_TrendGraphPanel);
			m_TrendGraphPanel.UpdateGraph();
		}

		private void allToolStripMenuItem_Click(object sender, EventArgs e)
		{
			SaveFileDialog sfd = new SaveFileDialog();
			sfd.Filter = "엑셀 파일 (*.xlsx)|*.xlsx|엑셀 파일 (*.xls)|*.xls|전체|*";
			DialogResult result = sfd.ShowDialog();
			string strFilePath = "";
			if (result == DialogResult.OK)
				strFilePath = sfd.FileName;
			else
				return;


			Cursor.Current = Cursors.WaitCursor;

			m_Report = new Report();
			m_Report.FilePath = strFilePath;
			m_Report.MaterialSpring = m_DataHandler.GetCurSpring();
			m_Report.MaterialTable = m_DataHandler.GetCurTable();
			m_Report.AnalysisData = m_AnalysisPanel.GetData();

			m_Report.RadarChart = m_RadarGraphPanel.CopyChart();

			m_Report.Export();

			Cursor.Current = Cursors.Default;

			//OpenPanel(m_TrendGraphPanel);
			//m_TrendGraphPanel.CopyChart(PanelTrendGraph.GRAPH_TYPE.HUMIDITY);
		}

		public void OnChartUpdate(Bitmap bm, PanelTrendGraph.GRAPH_TYPE Type)
		{
			switch(Type)
			{
				case PanelTrendGraph.GRAPH_TYPE.HUMIDITY:
					m_Report.TrendChartByHumidity = bm;
					m_TrendGraphPanel.CopyChart(PanelTrendGraph.GRAPH_TYPE.TEMPERATURE);
					break;

				case PanelTrendGraph.GRAPH_TYPE.TEMPERATURE:
					m_Report.TrendChartByTemperature = bm;
					m_TrendGraphPanel.CopyChart(PanelTrendGraph.GRAPH_TYPE.FORCE);
					break;

				case PanelTrendGraph.GRAPH_TYPE.FORCE:
					m_Report.TrendChartByForce = bm;
					m_TrendGraphPanel.CopyChart(PanelTrendGraph.GRAPH_TYPE.VELOCITY);
					break;

				case PanelTrendGraph.GRAPH_TYPE.VELOCITY:
					m_Report.TrendChartByVelocity = bm;
					m_Report.Export();
					m_Report = null;
					break;
			}
		}

		private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			AboutBox aboutWindow = new AboutBox();
			aboutWindow.ShowDialog();
		}
		#endregion



		#region Events from toolbox
		private void toolStripButtonOpen_Click(object sender, EventArgs e)
		{
			OpenDB();
		}

		private void toolStripButtonMaterial_Click(object sender, EventArgs e)
		{
			OpenPanel(m_MaterialPanel);
		}

		private void toolStripButtonResult_Click(object sender, EventArgs e)
		{
			OpenPanel(m_ResultPanel);
		}

		private void toolStripButtonAnalysis_Click(object sender, EventArgs e)
		{
			OpenPanel(m_AnalysisPanel);
		}

		private void toolStripButtonMap_Click(object sender, EventArgs e)
		{
			OpenPanel(m_CompatibilityPanel);
			m_CompatibilityPanel.UpdateData(m_DataHandler);
		}

		private void toolStripButtonRadarGraph_Click(object sender, EventArgs e)
		{
			OpenPanel(m_RadarGraphPanel);
			m_RadarGraphPanel.UpdateGraph();
		}

		private void toolStripButtonTrendGraph_Click(object sender, EventArgs e)
		{
			OpenPanel(m_TrendGraphPanel);
			m_TrendGraphPanel.UpdateGraph();
		}
	#endregion

		#region Events from panels
		public void OnApplyData(string strSpring, string strTable)
		{
			Console.WriteLine("[MainForm::OnApply] start");

			m_DataHandler.SetSelectedMaterial(strSpring, strTable);

			if (strTable == "All")
			{
				trendGraphToolStripMenuItem.Enabled = toolStripButtonTrendGraph.Enabled = false;
				radarGraphToolStripMenuItem.Enabled = toolStripButtonRadarGraph.Enabled = true;

				if (m_RadarGraphPanel != null)
					m_RadarGraphPanel.UpdateGraph();
				if (m_TrendGraphPanel != null)
					m_TrendGraphPanel.UpdateGraph();
			}
			else
			{
				trendGraphToolStripMenuItem.Enabled = toolStripButtonTrendGraph.Enabled = true;
				radarGraphToolStripMenuItem.Enabled = toolStripButtonRadarGraph.Enabled = false;

				if (m_TrendGraphPanel != null)
					m_TrendGraphPanel.UpdateGraph();
			}

			m_AnalysisPanel.UpdateData(m_DataHandler);

			Console.WriteLine("[MainForm::OnApply] end");
		}

		public void OnRadarSelectTable(string strTable)
		{
			if (m_AnalysisPanel != null)
				m_AnalysisPanel.SelectRow(strTable);
			OpenPanel(m_AnalysisPanel);
		}

		public void OnTrendSelectByHumidity(string strSpring, string strTable, float fHumidity, float fRPN)
		{
			if (m_ResultPanel != null)
				m_ResultPanel.SelectRowByHumidity(strSpring, strTable, fHumidity, fRPN);
			OpenPanel(m_ResultPanel);
		}

		public void OnTrendSelectByTemperature(string strSpring, string strTable, float fTemperature, float fRPN)
		{
			if (m_ResultPanel != null)
				m_ResultPanel.SelectRowByTemperature(strSpring, strTable, fTemperature, fRPN);
			OpenPanel(m_ResultPanel);
		}

		public void OnTrendSelectByForce(string strSpring, string strTable, float fTemperature, float fRPN)
		{
			if (m_ResultPanel != null)
				m_ResultPanel.SelectRowByForce(strSpring, strTable, fTemperature, fRPN);
			OpenPanel(m_ResultPanel);
		}

		public void OnTrendSelectByVelocity(string strSpring, string strTable, float fTemperature, float fRPN)
		{
			if (m_ResultPanel != null)
				m_ResultPanel.SelectRowByVelocity(strSpring, strTable, fTemperature, fRPN);
			OpenPanel(m_ResultPanel);
		}
		#endregion

		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			Config.OPTION.WindowBound = this.Bounds;
			Config.OPTION.CheckedColumns = m_ResultPanel.GetCheckedColumns();
			Config.OPTION.UncheckedColumns = m_ResultPanel.GetUncheckedColumns();
			Config.Save();
		}
	}
}
