using MaterialSkin;
using MaterialSkin.Controls;

namespace Elfisa.MaterialDemo;

/// <summary>
/// Demo: как может выглядеть «Электронная Фармация» на MaterialSkin.2 (отдельно от Elfisa.UI).
/// </summary>
internal sealed class PharmacyMaterialForm : MaterialForm
{
    private readonly MaterialTabControl _tabs;
    private readonly Panel _contentHost;
    private readonly MaterialLabel _statusLabel;

    public PharmacyMaterialForm()
    {
        Text = "ЭльФиСА — MaterialSkin demo";
        MinimumSize = new Size(960, 640);
        Size = new Size(1100, 720);
        StartPosition = FormStartPosition.CenterScreen;

        MaterialSkinManager.Instance.AddFormToManage(this);

        var header = new MaterialLabel
        {
            Text = "MaterialSkin.2 — демо для сравнения с Elfisa.UI",
            FontType = MaterialSkinManager.fontType.H6,
            Dock = DockStyle.Top,
            Height = 48,
            Padding = new Padding(24, 12, 24, 0)
        };

        _tabs = new MaterialTabControl
        {
            Dock = DockStyle.Top,
            Height = 48,
            Depth = 0
        };
        _tabs.TabPages.Add(new TabPage("Прайс-лист") { Tag = "price" });
        _tabs.TabPages.Add(new TabPage("Заказы") { Tag = "orders" });
        _tabs.TabPages.Add(new TabPage("Загрузить") { Tag = "upload" });
        _tabs.SelectedIndexChanged += (_, __) => ShowTab(_tabs.SelectedTab?.Tag as string ?? "price");

        _contentHost = new Panel
        {
            Dock = DockStyle.Fill,
            Padding = new Padding(24)
        };

        _statusLabel = new MaterialLabel
        {
            Dock = DockStyle.Bottom,
            Height = 36,
            Text = "Пользователь: demo@apteka (MaterialSkin dark / teal)",
            FontType = MaterialSkinManager.fontType.Caption,
            Padding = new Padding(24, 8, 24, 8)
        };

        Controls.Add(_contentHost);
        Controls.Add(_statusLabel);
        Controls.Add(_tabs);
        Controls.Add(header);

        ShowTab("price");
    }

    private void ShowTab(string key)
    {
        _contentHost.Controls.Clear();

        switch (key)
        {
            case "orders":
                _contentHost.Controls.Add(BuildOrdersView());
                break;
            case "upload":
                _contentHost.Controls.Add(BuildUploadView());
                break;
            default:
                _contentHost.Controls.Add(BuildPriceView());
                break;
        }
    }

    private Control BuildPriceView()
    {
        var root = new TableLayoutPanel
        {
            Dock = DockStyle.Fill,
            ColumnCount = 1,
            RowCount = 3
        };
        root.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        root.RowStyles.Add(new RowStyle(SizeType.Percent, 100f));
        root.RowStyles.Add(new RowStyle(SizeType.Absolute, 120f));

        var toolbar = new FlowLayoutPanel
        {
            Dock = DockStyle.Fill,
            AutoSize = true,
            WrapContents = false,
            Padding = new Padding(0, 0, 0, 16)
        };

        var search = new MaterialTextBox
        {
            Hint = "Поиск по наименованию…",
            Width = 320,
            Margin = new Padding(0, 8, 12, 0)
        };
        var clearBtn = new MaterialButton
        {
            Text = "×",
            Type = MaterialButton.MaterialButtonType.Outlined,
            Size = new Size(48, 36),
            Margin = new Padding(0, 8, 12, 0)
        };
        var supplier = new MaterialComboBox
        {
            Hint = "Поставщик",
            Width = 200,
            Margin = new Padding(0, 8, 0, 0)
        };
        supplier.Items.AddRange(new object[] { "Катрен", "Протек", "Все поставщики" });
        supplier.SelectedIndex = 0;

        toolbar.Controls.Add(search);
        toolbar.Controls.Add(clearBtn);
        toolbar.Controls.Add(supplier);

        var grid = new MaterialListView
        {
            Dock = DockStyle.Fill,
            FullRowSelect = true,
            View = View.Details,
            BorderStyle = BorderStyle.None
        };
        grid.Columns.Add("Наименование", 420);
        grid.Columns.Add("Производитель", 220);
        grid.Columns.Add("Цена", 90);
        grid.Columns.Add("Остаток", 90);
        grid.Items.Add(new ListViewItem(new[] { "Нурофен 200 мг №10", "Reckitt", "189,00", "42" }));
        grid.Items.Add(new ListViewItem(new[] { "Амоксициллин 500 мг №20", "Sandoz", "124,50", "18" }));
        grid.Items.Add(new ListViewItem(new[] { "Омез 20 мг №30", "Dr. Reddy's", "312,00", "7" }));

        var cartCard = new MaterialCard
        {
            Dock = DockStyle.Fill,
            Padding = new Padding(16)
        };
        var cartLayout = new TableLayoutPanel
        {
            Dock = DockStyle.Fill,
            ColumnCount = 4
        };
        cartLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 40f));
        cartLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20f));
        cartLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20f));
        cartLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20f));

        cartLayout.Controls.Add(new MaterialLabel
        {
            Text = "Корзина: 2 позиции · 313,50 ₽",
            FontType = MaterialSkinManager.fontType.Subtitle1,
            Dock = DockStyle.Fill
        }, 0, 0);

        cartLayout.Controls.Add(new MaterialButton
        {
            Text = "Сохранить заказ",
            Type = MaterialButton.MaterialButtonType.Contained,
            Dock = DockStyle.Fill,
            HighEmphasis = true
        }, 1, 0);

        cartLayout.Controls.Add(new MaterialButton
        {
            Text = "Очистить",
            Type = MaterialButton.MaterialButtonType.Outlined,
            Dock = DockStyle.Fill
        }, 2, 0);

        cartLayout.Controls.Add(new MaterialButton
        {
            Text = "Отправить",
            Type = MaterialButton.MaterialButtonType.Contained,
            Dock = DockStyle.Fill
        }, 3, 0);

        cartCard.Controls.Add(cartLayout);

        root.Controls.Add(toolbar, 0, 0);
        root.Controls.Add(grid, 0, 1);
        root.Controls.Add(cartCard, 0, 2);
        return root;
    }

    private Control BuildOrdersView()
    {
        var card = new MaterialCard
        {
            Dock = DockStyle.Fill,
            Padding = new Padding(24)
        };

        var title = new MaterialLabel
        {
            Text = "Журнал заказов",
            FontType = MaterialSkinManager.fontType.H5,
            Dock = DockStyle.Top,
            Height = 40
        };

        var grid = new MaterialListView
        {
            Dock = DockStyle.Fill,
            View = View.Details,
            FullRowSelect = true
        };
        grid.Columns.Add("№", 80);
        grid.Columns.Add("Дата", 120);
        grid.Columns.Add("Поставщик", 180);
        grid.Columns.Add("Статус", 140);
        grid.Columns.Add("Сумма", 100);
        grid.Items.Add(new ListViewItem(new[] { "1042", "14.07.2026", "Катрен", "Отправлен", "4 820,00" }));
        grid.Items.Add(new ListViewItem(new[] { "1041", "13.07.2026", "Протек", "Черновик", "1 240,00" }));

        card.Controls.Add(grid);
        card.Controls.Add(title);
        return card;
    }

    private Control BuildUploadView()
    {
        var card = new MaterialCard
        {
            Dock = DockStyle.Fill,
            Padding = new Padding(32)
        };

        var stack = new TableLayoutPanel
        {
            Dock = DockStyle.Fill,
            ColumnCount = 1,
            RowCount = 4
        };
        stack.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        stack.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        stack.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        stack.RowStyles.Add(new RowStyle(SizeType.Percent, 100f));

        stack.Controls.Add(new MaterialLabel
        {
            Text = "Загрузка данных с сервера",
            FontType = MaterialSkinManager.fontType.H5,
            AutoSize = true,
            Margin = new Padding(0, 0, 0, 8)
        }, 0, 0);

        stack.Controls.Add(new MaterialLabel
        {
            Text = "MaterialSkin даёт готовые карточки, ripple-кнопки и тёмную тему из коробки.",
            FontType = MaterialSkinManager.fontType.Body1,
            AutoSize = true,
            Margin = new Padding(0, 0, 0, 24)
        }, 0, 1);

        var buttons = new FlowLayoutPanel { AutoSize = true, WrapContents = false };
        buttons.Controls.Add(new MaterialButton
        {
            Text = "Загрузить прайс",
            Type = MaterialButton.MaterialButtonType.Contained,
            HighEmphasis = true,
            Margin = new Padding(0, 0, 12, 0)
        });
        buttons.Controls.Add(new MaterialButton
        {
            Text = "Отправить заказы",
            Type = MaterialButton.MaterialButtonType.Outlined
        });
        stack.Controls.Add(buttons, 0, 2);

        card.Controls.Add(stack);
        return card;
    }
}
