using System;
using System.Drawing;
using System.Windows.Forms;
using Elfisa.UI.Theming;

namespace Elfisa.UI.Controls
{
    public class ModernDataGridView : DataGridView
    {
        private ThemeColors _theme;

        public ModernDataGridView()
        {
            EnableHeadersVisualStyles = false;
            BorderStyle = BorderStyle.None;
            CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;
            RowHeadersVisible = false;
            SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            MultiSelect = false;
            AllowUserToAddRows = false;
            AllowUserToResizeRows = false;
            AllowUserToResizeColumns = true;
            AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
            ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            DoubleBuffered = true;
            ApplyTheme(ThemeManager.Colors);
        }

        public void ApplyTheme(ThemeColors theme)
        {
            _theme = theme;
            BackgroundColor = theme.GridRowBackground;
            GridColor = theme.GridBorder;
            ColumnHeadersHeight = theme.GridHeaderHeight;
            RowTemplate.Height = theme.GridRowHeight;
            Font = theme.FontRegular13;
            ColumnHeadersDefaultCellStyle = new DataGridViewCellStyle
            {
                BackColor = theme.GridHeaderBackground,
                ForeColor = theme.GridHeaderForeground,
                Font = theme.FontSemibold14,
                Alignment = DataGridViewContentAlignment.MiddleLeft,
                Padding = new Padding(12, 0, 12, 0),
                SelectionBackColor = theme.GridHeaderBackground,
                SelectionForeColor = theme.GridHeaderForeground
            };
            DefaultCellStyle = new DataGridViewCellStyle
            {
                BackColor = theme.GridRowBackground,
                ForeColor = theme.TextPrimary,
                SelectionBackColor = theme.GridRowSelected,
                SelectionForeColor = theme.GridRowSelectedText,
                Font = theme.FontRegular13,
                Padding = new Padding(12, 0, 12, 0),
                WrapMode = DataGridViewTriState.False
            };
            AlternatingRowsDefaultCellStyle = new DataGridViewCellStyle
            {
                BackColor = theme.GridRowAlternate,
                ForeColor = theme.TextPrimary,
                SelectionBackColor = theme.GridRowSelected,
                SelectionForeColor = theme.GridRowSelectedText,
                Font = theme.FontRegular13,
                Padding = new Padding(12, 0, 12, 0)
            };
            Invalidate();
        }

        protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
        {
            base.OnCellMouseEnter(e);
            if (e.RowIndex >= 0 && e.RowIndex != CurrentCell?.RowIndex)
            {
                var theme = _theme ?? ThemeManager.Colors;
                Rows[e.RowIndex].DefaultCellStyle.BackColor = theme.GridRowHover;
            }
        }

        protected override void OnCellMouseLeave(DataGridViewCellEventArgs e)
        {
            base.OnCellMouseLeave(e);
            if (e.RowIndex >= 0)
            {
                var theme = _theme ?? ThemeManager.Colors;
                var isAlt = e.RowIndex % 2 == 1;
                Rows[e.RowIndex].DefaultCellStyle.BackColor = isAlt
                    ? theme.GridRowAlternate
                    : theme.GridRowBackground;
            }
        }
    }
}
