using Elwig.Helpers;
using Elwig.Models.Entities;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;

namespace Elwig.Windows {
    public partial class BaseDataWindow {

        private Dictionary<string, string?>? _attrs = null;
        private Dictionary<WineAttr, string>? _attrIds = null;
        private ObservableCollection<WineAttr>? _attrList = null;
        private bool _attrChanged = false;
        private bool _attrUpdate = false;

        private void WineAttributesInitEditing() {
            _attrList = new(Context.WineAttributes.OrderBy(a => a.Name).ToList());
            _attrs = _attrList.ToDictionary(a => a.AttrId, a => (string?)a.AttrId);
            _attrIds = _attrList.ToDictionary(a => a, a => a.AttrId);
            ControlUtils.RenewItemsSource(WineAttributeList, _attrList, a => (a as WineAttr)?.AttrId);
            WineAttributeList_SelectionChanged(null, null);
        }

        private void WineAttributesFinishEditing() {
            ControlUtils.RenewItemsSource(WineAttributeList, Context.WineAttributes.OrderBy(a => a.Name).ToList(), a => (a as WineAttr)?.AttrId);
            _attrList = null;
            _attrs = null;
            _attrIds = null;
            _attrChanged = false;

            WineAttributeAddButton.IsEnabled = false;
            WineAttributeDeleteButton.IsEnabled = false;
        }

        private async Task WineAttributesSave() {
            if (!_attrChanged || _attrList == null || _attrs == null || _attrIds == null)
                return;

            foreach (var (attrid, _) in _attrs.Where(a => a.Value == null)) {
                Context.Remove(Context.WineAttributes.Find(attrid));
            }
            foreach (var (attr, old) in _attrIds) {
                attr.AttrId = old;
            }
            foreach (var (old, attrid) in _attrs.Where(a => a.Value != null)) {
                Context.Update(Context.WineAttributes.Find(old));
            }
            await Context.SaveChangesAsync();

            foreach (var (old, attrid) in _attrs.Where(a => a.Value != null)) {
                await Context.Database.ExecuteSqlAsync($"UPDATE wine_attribute SET attrid = {attrid} WHERE attrid = {old}");
            }
            await Context.SaveChangesAsync();

            foreach (var attr in _attrList.Where(a => !_attrIds.ContainsKey(a))) {
                if (attr.AttrId == null) continue;
                await Context.AddAsync(attr);
            }
            await Context.SaveChangesAsync();
        }

        private void WineAttributeList_SelectionChanged(object? sender, SelectionChangedEventArgs? evt) {
            UpdateButtons();
            _attrUpdate = true;
            if (WineAttributeList.SelectedItem is not WineAttr attr) {
                WineAttributeIdInput.Text = "";
                WineAttributeNameInput.Text = "";
                WineAttributeActiveInput.IsChecked = false;
                WineAttributeMaxKgPerHaInput.Text = "";
                WineAttributeStrictInput.IsChecked = false;
                WineAttributeFillLowerInput.SelectedItem = null;
            } else {
                WineAttributeIdInput.Text = attr.AttrId;
                WineAttributeNameInput.Text = attr.Name;
                WineAttributeActiveInput.IsChecked = attr.IsActive;
                WineAttributeMaxKgPerHaInput.Text = attr.MaxKgPerHa?.ToString() ?? "";
                WineAttributeStrictInput.IsChecked = attr.IsStrict;
                WineAttributeFillLowerInput.SelectedIndex = attr.FillLower;
            }
            _attrUpdate = false;
        }

        private void WineAttributeAddButton_Click(object sender, RoutedEventArgs evt) {
            if (_attrList == null) return;
            _attrChanged = true;
            var item = Context.CreateProxy<WineAttr>();
            _attrList.Add(item);
            WineAttributeList.SelectedItem = item;
            UpdateButtons();
        }

        private void WineAttributeDeleteButton_Click(object sender, RoutedEventArgs evt) {
            if (_attrList == null || _attrs == null) return;
            _attrChanged = true;
            var idx = WineAttributeList.SelectedIndex;
            var item = _attrList[idx];
            _attrs[item.AttrId] = null;
            _attrList.RemoveAt(idx);
            WineAttributeList.SelectedIndex = idx < _attrList.Count ? idx : idx - 1;
            UpdateButtons();
        }

        private void WineAttribute_Changed(object? sender, RoutedEventArgs? evt) {
            if (_attrUpdate || (!IsEditing && !IsCreating) || WineAttributeList.SelectedItem is not WineAttr attr || _attrs == null || _attrIds == null) return;
            _attrChanged = _attrChanged ||
                WineAttributeIdInput.Text != attr.AttrId ||
                WineAttributeNameInput.Text != attr.Name ||
                WineAttributeActiveInput.IsChecked != attr.IsActive ||
                WineAttributeMaxKgPerHaInput.Text != attr.MaxKgPerHa?.ToString() ||
                WineAttributeStrictInput.IsChecked != attr.IsStrict ||
                WineAttributeFillLowerInput.SelectedIndex != attr.FillLower;

            var old = _attrIds.GetValueOrDefault(attr);
            var id = WineAttributeIdInput.Text;
            if (old != null) _attrs[old] = id;
            attr.AttrId = id;
            attr.Name = WineAttributeNameInput.Text ?? "";
            attr.IsActive = WineAttributeActiveInput.IsChecked ?? false;
            attr.MaxKgPerHa = WineAttributeMaxKgPerHaInput.Text.Length > 0 ? int.Parse(WineAttributeMaxKgPerHaInput.Text) : null;
            attr.IsStrict = WineAttributeStrictInput.IsChecked ?? false;
            attr.FillLower = WineAttributeFillLowerInput.SelectedIndex;

            CollectionViewSource.GetDefaultView(_attrList).Refresh();
            CollectionViewSource.GetDefaultView(_attrList).Refresh();
            UpdateButtons();
        }

        private void WineAttributeIdInput_TextChanged(object sender, TextChangedEventArgs evt) {
            UpperCaseInput_TextChanged(sender, evt);
            WineAttribute_Changed(sender, evt);
        }

        private void WineAttributeMaxKgPerHaInput_TextChanged(object sender, TextChangedEventArgs evt) {
            InputTextChanged((TextBox)sender, Validator.CheckInteger((TextBox)sender, false, 5));
            WineAttribute_Changed(sender, evt);
        }

        private void WineAttributeStrictInput_Changed(object sender, RoutedEventArgs evt) {
            WineAttributeFillLowerInput.Visibility = WineAttributeStrictInput.IsChecked == true ? Visibility.Visible : Visibility.Hidden;
            WineAttributeFillLowerLabel.Visibility = WineAttributeFillLowerInput.Visibility;
            WineAttribute_Changed(sender, evt);
        }
    }
}
