using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Components.Visual;

namespace Components
{
    public partial class UserControlTable : UserControl
    {
        public int index
        {
            get => dataGridViewTable.SelectedRows.Count > 0 ?  dataGridViewTable.SelectedRows[0].Index : -1;
            set
            {
                if (value >= 0 && value < dataGridViewTable.Rows.Count)
                {
                    dataGridViewTable.ClearSelection();
                    dataGridViewTable.Rows[value].Selected = true;
                }
            }
        }
        public UserControlTable()
        {
            InitializeComponent();
        }

        public void ConfigureColumns(List<ColumnInfo> columnInfo)
        {
            dataGridViewTable.Columns.Clear();
            for (int i = 0; i < columnInfo.Count; i++)
            {
                DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn
                {
                    Name = columnInfo[i].Name,
                    HeaderText = columnInfo[i].HeaderText,
                    Width = columnInfo[i].Width,
                    Visible = columnInfo[i].Visible,
                    DataPropertyName = columnInfo[i].DataPropertyName,
                };
                dataGridViewTable.Columns.Add(column);
            }   
        }

        public void ClearRows()
        {
            dataGridViewTable.Rows.Clear();
        }

        public void AddList<T>(List<T> values)
        {
            ClearRows();
            var props = typeof(T).GetProperties();

            foreach (T value in values)
            {
                int newRowInd = dataGridViewTable.Rows.Add();
                foreach (DataGridViewColumn col in dataGridViewTable.Columns)
                {
                    var prop = props.FirstOrDefault(x => x.Name == col.DataPropertyName)
                        ?? throw new InvalidOperationException($"No property {col.DataPropertyName} found in type {typeof(T).Name}");
                    var val = prop.GetValue(value);
                    dataGridViewTable.Rows[newRowInd].Cells[col.Index].Value = val;
                }
            }
        }

        public T GetObjectFromRow<T>()
            where T : new()
        {
            if (dataGridViewTable.SelectedRows.Count == 0)
            {
                throw new InvalidOperationException("At least one row must be selected");
            }

            T returnObject = new();

            var selectedRow = dataGridViewTable.SelectedRows[0];
            var props = typeof(T).GetProperties();
            var cells = dataGridViewTable.Rows[selectedRow.Index].Cells;

            for (int i = 0; i < dataGridViewTable.ColumnCount; i++)
            {
                var curCell = cells[i];
                var prop = props.FirstOrDefault(x => x.Name == dataGridViewTable.Columns[i].DataPropertyName);
                prop?.SetValue(returnObject, curCell.Value);
            }
            return returnObject;
        }
    }
}