using InABox.Core;
using Org.BouncyCastle.Bcpg;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace InABox.DynamicGrid;

/// <summary>
/// Interaction logic for DynamicFormDesignWindow.xaml
/// </summary>
public partial class DynamicFormDesignWindow : Window, IDynamicFormWindow
{

    public DynamicFormDesignWindow() : base()
    {
        InitializeComponent();

        Preview.Mode = FormMode.Designing;
    }

    public DynamicFormDesignGrid Grid => Preview;

    private DFLayoutType _type;
    public DFLayoutType Type
    {
        get => _type;
        set
        {
            _type = value;
            Width = _type == DFLayoutType.Mobile ? 600 : 1000;
            Height = 800;
        }
    }

    public bool Designing
    {
        get => Grid.Mode == FormMode.Designing;
        set
        {
            Grid.Mode = value
                ? FormMode.Designing
                : FormMode.Preview;
            SwitchView.Content = value ? "Preview" : "Design";
        }
    }

    public event DynamicFormDesignGrid.CreateVariableHandler OnCreateVariable
    {
        add => Grid.OnCreateVariable += value;
        remove => Grid.OnCreateVariable -= value;
    }

    public event DynamicFormDesignGrid.EditVariableHandler OnEditVariable
    {
        add => Grid.OnEditVariable += value;
        remove => Grid.OnEditVariable -= value;
    }

    public string SaveLayout()
    {
        return Grid.Form.SaveLayout();
    }

    private void SwitchView_Click(object sender, RoutedEventArgs e)
    {
        Designing = !Designing;
    }

    private void OK_Click(object sender, RoutedEventArgs e)
    {
        DialogResult = true;
    }

    private void Cancel_Click(object sender, RoutedEventArgs e)
    {
        DialogResult = false;
    }

    private void DynamicFormWindow_KeyDown(object sender, KeyEventArgs e)
    {
        Grid.HandleKeyDown(e);
    }
}
