using InABox.Core;
using InABox.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace InABox.DynamicGrid
{
    public abstract class DynamicFormWindow : ThemableWindow
    {
        protected abstract DynamicFormDesignGrid Grid { get; }

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

        public DFLayout Form
        {
            get => Grid.Form;
            set => Grid.Form = value;
        }

        public IList<DigitalFormVariable> Variables
        {
            get => Grid.Variables;
            set => Grid.Variables = value;
        }

        /// <summary>
        /// Renders the form; after this is called, any changes to properties triggers a refresh.
        /// </summary>
        public void Initialize()
        {
            Grid.Initialize();
        }

        public void LoadLayout(DigitalFormLayout layout, IList<DigitalFormVariable> variables)
        {
            Variables = variables;

            var f = new DFLayout();
            if (!string.IsNullOrWhiteSpace(layout.Layout))
            {
                f.LoadLayout(layout.Layout);
            }
            else
            {
                f = new DFLayout();
                f.RowHeights.Add("Auto");
                f.ColumnWidths.AddRange(new[] { "*", "Auto" });
            }

            f.LoadVariables(variables);

            Form = f;
        }


        /*public static T LoadDigitalForm<T>(Guid id) where T : Entity, IPersistent, IRemotable, IDigitalFormInstance, new()
        {
            var result = new Client<T>().Query(
                new Filter<T>(x => x.ID).IsEqualTo(id),
                new Columns<T>(
                    x => x.ID,
                    x => x.Form.ID,
                    x => x.Form.Description,
                    x => x.FormCompleted,
                    x => x.FormCompletedBy.ID,
                    x => x.FormCompletedBy.UserID,
                    x => x.FormData
                )
            ).Rows.FirstOrDefault()?.ToObject<T>();
            return result;
        }*/
    }
}
