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 WeifenLuo.WinFormsUI.Docking;

namespace friction
{
	public partial class PanelAnalysis : DockContent
	{
		MainForm m_Owner = null;

		string m_CurSpring = "";

		public PanelAnalysis(MainForm owner)
		{
			InitializeComponent();

			m_Owner = owner;

			Theme.Apply(this);
			Theme.Apply(dgvAnalysis);

			lbInfo11.BackColor = Theme.Green;
			lbInfo12.BackColor = Theme.Yellow;
			lbInfo13.BackColor = Theme.Red;
			lbInfo21.BackColor = Theme.Green;
			lbInfo22.BackColor = Theme.Yellow;
			lbInfo23.BackColor = Theme.Red;
			lbInfo24.BackColor = Theme.Gray;
		}

		public void Reset()
		{
			m_CurSpring = "";
		}

		private Color GetDependancyColor(float fValue, int iCnt)
		{
			Config.ANALYSIS.DEPENDANCY Type = Config.ANALYSIS.GetDependancy(fValue, iCnt);
			switch(Type)
			{
				case Config.ANALYSIS.DEPENDANCY.NO:
					return Theme.Green;

				case Config.ANALYSIS.DEPENDANCY.POTENTIAL:
					return Theme.Yellow;

				case Config.ANALYSIS.DEPENDANCY.HIGH:
					return Theme.Red;

				case Config.ANALYSIS.DEPENDANCY.NOT_ENNOUGH_DATA:
					return Theme.Gray;
			}

			return Theme.White;
		}

		public void UpdateData(DataHandler data)
		{
			string strSpring = data.GetCurSpring();

			if (strSpring == m_CurSpring)
				return;

			lbSpring.Text = "Material Spring: " +strSpring;
			lbSpring.ForeColor = Color.Orange;

			dgvAnalysis.Columns.Clear();
			dgvAnalysis.Rows.Clear();

			dgvAnalysis.DefaultCellStyle.Format = "N2";

			dgvAnalysis.Columns.Add("chTable", "Table");
			dgvAnalysis.Columns.Add("chNoTest", "No. of Tests");
			dgvAnalysis.Columns.Add("chAvg", "Average RPN");
			dgvAnalysis.Columns.Add("chSTD", "Standard Deviation");
			dgvAnalysis.Columns.Add("chForce", "Normal Force");
			dgvAnalysis.Columns.Add("chTemp", "Temperature");
			dgvAnalysis.Columns.Add("chHumi", "Rel. Humidity");
			dgvAnalysis.Columns.Add("chVel", "Velocity");

			foreach (string strTable in data.GetTableList())
			{
				DataHandler.CalcResult result = data.GetCalc(strSpring, strTable);
				if (result.m_iCnt <= 0)
					continue;

				int iIdx = dgvAnalysis.Rows.Add(strTable, result.m_iCnt, result.m_fAvgRPN, result.m_fStdRPN, result.m_fDiffByForce, result.m_fDiffByTemp, result.m_fDiffByHumid, result.m_fDiffByVel);


				Config.ANALYSIS.RISK RiskType = Config.ANALYSIS.GetRisk(result.m_fAvgRPN);
				Config.ANALYSIS.DEPENDANCY DependancyType = Config.ANALYSIS.GetDependancy(result.m_fStdRPN, result.m_iCnt);

				if(DependancyType == Config.ANALYSIS.DEPENDANCY.NOT_ENNOUGH_DATA)
				{
					for (int i = 2; i <= 7; i++)
					{
						dgvAnalysis.Rows[iIdx].Cells[i].Style.BackColor = Theme.Gray;
						dgvAnalysis.Rows[iIdx].Cells[i].Style.ForeColor = Theme.Backcolor;
					}
				}
				else
				{
					switch (RiskType)
					{
						case Config.ANALYSIS.RISK.NO:
							dgvAnalysis.Rows[iIdx].Cells[2].Style.BackColor = Theme.Green;
							break;

						case Config.ANALYSIS.RISK.POTENTIAL:
							dgvAnalysis.Rows[iIdx].Cells[2].Style.BackColor = Theme.Yellow;
							break;

						case Config.ANALYSIS.RISK.HIGH:
							dgvAnalysis.Rows[iIdx].Cells[2].Style.BackColor = Theme.Red;
							break;
					}

					dgvAnalysis.Rows[iIdx].Cells[3].Style.BackColor = GetDependancyColor(result.m_fStdRPN, result.m_iCnt);
					dgvAnalysis.Rows[iIdx].Cells[4].Style.BackColor = GetDependancyColor(result.m_fDiffByForce, result.m_iCnt);
					dgvAnalysis.Rows[iIdx].Cells[5].Style.BackColor = GetDependancyColor(result.m_fDiffByTemp, result.m_iCnt);
					dgvAnalysis.Rows[iIdx].Cells[6].Style.BackColor = GetDependancyColor(result.m_fDiffByHumid, result.m_iCnt);
					dgvAnalysis.Rows[iIdx].Cells[7].Style.BackColor = GetDependancyColor(result.m_fDiffByVel, result.m_iCnt);
				}
			}

			m_CurSpring = strSpring;
		}

		public void SelectRow(string strTable)
		{
			dgvAnalysis.ClearSelection();
			foreach (DataGridViewRow row in dgvAnalysis.Rows)
			{
				if ((string)row.Cells["chTable"].Value == strTable)
				{
					row.Selected = true;
					break;
				}
			}
		}

		public DataTable GetData()
		{
			var dt = new DataTable();
			foreach (DataGridViewColumn column in dgvAnalysis.Columns)
			{
				if (column.Visible)
					dt.Columns.Add(column.HeaderText);
			}

			object[] cellValues = new object[dgvAnalysis.Columns.Count];
			foreach (DataGridViewRow row in dgvAnalysis.Rows)
			{
				for (int i = 0; i < row.Cells.Count; i++)
					cellValues[i] = row.Cells[i].Value;
				dt.Rows.Add(cellValues);
			}

			return dt;
		}
	}
}
