using InABox.Core;
using InABox.DynamicGrid;
using InABox.WPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace InABox.Poster.MYOB;

public class MYOBCompanyFileEditor : BaseEditor
{
    protected override BaseEditor DoClone()
    {
        return new MYOBCompanyFileEditor();
    }
}

public class MYOBCompanyFileEditorControl : DynamicEnclosedEditorControl<MYOBCompanyFile, MYOBCompanyFileEditor>
{
    private Grid Grid = null!;
    private TextBox TextBox = null!;
    private Button Select = null!;
    private Button Clear = null!;

    private readonly MYOBCompanyFile Value = new();
    private static readonly Column<MYOBCompanyFile> IDColumn = new(x => x.ID);
    private static readonly Column<MYOBCompanyFile> NameColumn = new(x => x.Name);

    public override int DesiredHeight() => 25;

    public override int DesiredWidth() => int.MaxValue;

    protected override FrameworkElement CreateEditor()
    {
        Grid = new Grid
        {
            VerticalAlignment = VerticalAlignment.Stretch,
            HorizontalAlignment = HorizontalAlignment.Stretch
        };
        Grid.AddColumn(GridUnitType.Star);
        Grid.AddColumn(70);
        Grid.AddColumn(70);

        TextBox = new TextBox
        {
            VerticalAlignment = VerticalAlignment.Stretch,
            VerticalContentAlignment = VerticalAlignment.Center,
            HorizontalAlignment = HorizontalAlignment.Stretch,
            IsEnabled = false
        };
        Grid.AddChild(TextBox, 0, 0);

        Select = new Button
        {
            VerticalAlignment = VerticalAlignment.Stretch,
            VerticalContentAlignment = VerticalAlignment.Center,
            HorizontalAlignment = HorizontalAlignment.Stretch,
            Content = "Select",
            Margin = new Thickness(5, 0, 0, 0),
        };
        Select.Click += Select_Click;
        Grid.AddChild(Select, 0, 1);

        Clear = new Button
        {
            VerticalAlignment = VerticalAlignment.Stretch,
            VerticalContentAlignment = VerticalAlignment.Center,
            HorizontalAlignment = HorizontalAlignment.Stretch,
            Content = "Clear",
            Margin = new Thickness(5, 0, 0, 0),
            Focusable = false
        };
        Clear.Click += Clear_Click;
        Grid.AddChild(Clear, 0, 2);

        return Grid;
    }

    public override void Configure()
    {
    }

    private void Select_Click(object sender, RoutedEventArgs e)
    {
        if (e.OriginalSource != Select) return;

        var file = MYOBCompanyFileSelectionDialog.SelectCompanyFile();
        if(file is not null)
        {
            Value.ID = file.Id;
            Value.Name = file.Name;
            TextBox.Text = Value.Name;
            CheckChanged();
        }
    }

    private void Clear_Click(object sender, RoutedEventArgs e)
    {
        Value.ID = Guid.Empty;
        Value.Name = "";
        TextBox.Text = "";
        CheckChanged();
    }

    protected override object? GetChildValue(string property)
    {
        if (IDColumn.IsEqualTo(property)) return Value.ID;
        if (NameColumn.IsEqualTo(property)) return Value.Name;
        return null;
    }

    protected override void SetChildValue(string property, object? value)
    {
        if (IDColumn.IsEqualTo(property)) Value.ID = (Guid)(value ?? Guid.Empty);
        else if (NameColumn.IsEqualTo(property))
        {
            Value.Name = (value as string) ?? "";
            TextBox.Text = Value.Name;
        }
    }

    protected override IEnumerable<KeyValuePair<string, object?>> GetChildValues()
    {
        yield return new(IDColumn.Property, Value.ID);
        yield return new(NameColumn.Property, Value.Name);
    }

    public override void SetColor(Color color)
    {
        TextBox.Background = IsEnabled ? new SolidColorBrush(color) : new SolidColorBrush(Colors.WhiteSmoke);
    }

    public override void SetFocus()
    {
        Select.Focus();
    }
}
