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 PanelResult : DockContent
	{
		MainForm m_Owner = null;

		public PanelResult(MainForm owner)
		{
			InitializeComponent();

			m_Owner = owner;

			Theme.Apply(this);
			Theme.Apply(dgvData);
			Theme.Apply(lvColumn);

			dgvData.CellFormatting += DgvData_CellFormatting;
		}

		private void DgvData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			DataGridViewRow row = dgvData.Rows[e.RowIndex];
			if(e.ColumnIndex == row.Cells[Config.COLUMN_NAME.RPN].ColumnIndex)
			{
				if ((float)e.Value <= 3)
					e.CellStyle.BackColor = Theme.Green;
				else if ((float)e.Value <= 5)
					e.CellStyle.BackColor = Theme.Yellow;
				else
					e.CellStyle.BackColor = Theme.Red;
			}
		}

		public void UpdateData(DataHandler data)
		{
			dgvData.DataSource = data.GetData();
			dgvData.DefaultCellStyle.Format = "N2";

			if(lvColumn.Items.Count > 0)
			{
				Config.OPTION.CheckedColumns = GetCheckedColumns();
				Config.OPTION.UncheckedColumns = GetUncheckedColumns();
			}

			lvColumn.Items.Clear();

			List<List<string>> Columns = new List<List<string>>() { data.GetActiveColumns(), data.GetNonactiveColumns() };
			for(int iGroup=0; iGroup<Columns.Count; iGroup++)
			{
				foreach(string col in Columns[iGroup])
				{
					ListViewItem item = new ListViewItem(new string[] { col });
					item.Text = col;

					if(Config.OPTION.CheckedColumns != null && Config.OPTION.CheckedColumns.Exists(s => s==col) == true)
						item.Checked = true;
					else if(Config.OPTION.UncheckedColumns != null && Config.OPTION.UncheckedColumns.Exists(s => s == col) == true)
						item.Checked = false;
					else if (iGroup == 0)
						item.Checked = true;
					else
						item.Checked = false;

					item.ForeColor = (iGroup == 0) ? Theme.Forecolor : Theme.Gray;

					lvColumn.Items.Add(item);
				}
			}
		}

		private void lvColumn_ItemChecked(object sender, ItemCheckedEventArgs e)
		{
			string strColName = e.Item.SubItems[0].Text;
			bool bShow = e.Item.Checked;

			dgvData.Columns[strColName].Visible = bShow;
		}

		private void lvColumn_SizeChanged(object sender, EventArgs e)
		{
			Theme.ResizeFullColumn(lvColumn);
		}

		public void SelectRowByHumidity(string strSpring, string strTable, float fHumidity, float fRPN)
		{
			dgvData.ClearSelection();

			DataGridViewRow row = dgvData.Rows
									.Cast<DataGridViewRow>()
									.Where(r =>
										r.Cells[Config.COLUMN_NAME.SPRING].Value.Equals(strSpring) &&
										r.Cells[Config.COLUMN_NAME.TABLE].Value.Equals(strTable) &&
										r.Cells[Config.COLUMN_NAME.RPN].Value.Equals(fRPN) &&
										r.Cells[Config.COLUMN_NAME.HUMIDITY].Value.Equals(fHumidity)
										)
									.First();

			if (row != null)
			{
				row.Selected = true;
				dgvData.FirstDisplayedScrollingRowIndex = row.Index;
			}
		}

		public void SelectRowByTemperature(string strSpring, string strTable, float fTemperature, float fRPN)
		{
			dgvData.ClearSelection();

			DataGridViewRow row = dgvData.Rows
									.Cast<DataGridViewRow>()
									.Where(r =>
										r.Cells[Config.COLUMN_NAME.SPRING].Value.Equals(strSpring) &&
										r.Cells[Config.COLUMN_NAME.TABLE].Value.Equals(strTable) &&
										r.Cells[Config.COLUMN_NAME.RPN].Value.Equals(fRPN) &&
										r.Cells[Config.COLUMN_NAME.TEMP].Value.Equals(fTemperature)
										)
									.First();

			if (row != null)
			{
				row.Selected = true;
				dgvData.FirstDisplayedScrollingRowIndex = row.Index;
			}
		}

		public void SelectRowByForce(string strSpring, string strTable, float fForce, float fRPN)
		{
			dgvData.ClearSelection();

			DataGridViewRow row = dgvData.Rows
									.Cast<DataGridViewRow>()
									.Where(r =>
										r.Cells[Config.COLUMN_NAME.SPRING].Value.Equals(strSpring) &&
										r.Cells[Config.COLUMN_NAME.TABLE].Value.Equals(strTable) &&
										r.Cells[Config.COLUMN_NAME.RPN].Value.Equals(fRPN) &&
										r.Cells[Config.COLUMN_NAME.FORCE].Value.Equals(fForce)
										)
									.First();

			if (row != null)
			{
				row.Selected = true;
				dgvData.FirstDisplayedScrollingRowIndex = row.Index;
			}
		}

		public void SelectRowByVelocity(string strSpring, string strTable, float fVelocity, float fRPN)
		{
			dgvData.ClearSelection();

			DataGridViewRow row = dgvData.Rows
									.Cast<DataGridViewRow>()
									.Where(r =>
										r.Cells[Config.COLUMN_NAME.SPRING].Value.Equals(strSpring) &&
										r.Cells[Config.COLUMN_NAME.TABLE].Value.Equals(strTable) &&
										r.Cells[Config.COLUMN_NAME.RPN].Value.Equals(fRPN) &&
										r.Cells[Config.COLUMN_NAME.VELOCITY].Value.Equals(fVelocity)
										)
									.First();

			if (row != null)
			{
				row.Selected = true;
				dgvData.FirstDisplayedScrollingRowIndex = row.Index;
			}
		}

		public List<string> GetCheckedColumns()
		{
			List<string> result = new List<string>();
			foreach(ListViewItem item in lvColumn.Items)
			{
				if (item.Checked == true)
					result.Add(item.Text);
			}

			return result;
		}

		public List<string> GetUncheckedColumns()
		{
			List<string> result = new List<string>();
			foreach (ListViewItem item in lvColumn.Items)
			{
				if (item.Checked == false)
					result.Add(item.Text);
			}

			return result;
		}
	}
}
