using Microsoft.UI.Xaml.Controls;
using Sonex.Data.Records;
using System;
using System.Globalization;

namespace Sonex.Client.Views
{
    public sealed partial class ProductDimensionsView : UserControl, IView
    {
        private string? _currentKey;
        private bool _hasChanges;

        public bool HasChanges => _hasChanges;
        public bool CanSave => CanEdit && HasChanges;
        public bool CanEdit => false;

        public ProductDimensionsView()
        {
            InitializeComponent();
            ClearProductValues();
        }

        public void Clear()
        {
            _currentKey = null;
            ClearProductValues();
            MarkSaved();
        }

        public void Reload()
        {
            if (string.IsNullOrWhiteSpace(_currentKey))
            {
                Clear();
                return;
            }

            Load(_currentKey);
        }

        public void Load(string key)
        {
            _currentKey = key;
            var product = ProductRecord.GetDimensionsInformation(key).GetAwaiter().GetResult();

            if (product.Success && product.Item is not null)
            {
                LoadProductValues(product.Item);
            }
            else
            {
                ClearProductValues();
                ArticleNumberProperty.Value = key;
            }

            MarkSaved();
        }

        public void Save()
        {
            MarkSaved();
        }

        public void MarkChanged()
        {
            _hasChanges = true;
        }

        public void MarkSaved()
        {
            _hasChanges = false;
        }

        private void LoadProductValues(ProductRecord product)
        {
            ArticleNameProperty.Value = product.ArticleName ?? string.Empty;
            ArticleNumberProperty.Value = product.Artnr;
            DimensionsCheckedProperty.Value = DateText(product.ArticleDimensionsChecked);

            SuPackagingProperty.Value = product.PackagingSu ?? string.Empty;
            SuQuantityProperty.Value = Text(product.SuQuantity);
            SuWeightGrossProperty.Value = Text(product.SuWeightGross);
            SuWeightNetProperty.Value = Text(product.SuWeightNet);
            SuLengthProperty.Value = Text(product.SuLength);
            SuHeightProperty.Value = Text(product.SuHeight);
            SuWidthProperty.Value = Text(product.SuWidth);
            SuVolumeProperty.Value = Text(product.SuVolume);
            SuStackLimitProperty.Value = Text(product.SuStackLimit);
            SuNestVolumeProperty.Value = Text(product.SuNestVolume);
            SuNestMaxProperty.Value = Text(product.SuNestMax);
            SuStackabilityProperty.Value = product.SuStackability ?? string.Empty;
            SuFragileProperty.Value = product.SuFragile ?? string.Empty;
            SuKeepUprightProperty.Value = product.SuKeepUpright ?? string.Empty;

            PuPackagingProperty.Value = product.PackagingPu ?? string.Empty;
            PuQuantityProperty.Value = Text(product.PuQuantity);
            PuWeightGrossProperty.Value = Text(product.PuWeightGross);
            PuWeightNetProperty.Value = Text(product.PuWeightNet);
            PuLengthProperty.Value = Text(product.PuLength);
            PuHeightProperty.Value = Text(product.PuHeight);
            PuWidthProperty.Value = Text(product.PuWidth);
            PuVolumeProperty.Value = Text(product.PuVolume);
            PuStackLimitProperty.Value = Text(product.PuStackLimit);
            PuNestVolumeProperty.Value = Text(product.PuNestVolume);
            PuNestMaxProperty.Value = Text(product.PuNestMax);
            PuStackabilityProperty.Value = product.PuStackability ?? string.Empty;
            PuFragileProperty.Value = product.PuFragile ?? string.Empty;
            PuKeepUprightProperty.Value = product.PuKeepUpright ?? string.Empty;

            HnPackagingProperty.Value = product.PackagingHn ?? string.Empty;
            HnQuantityProperty.Value = Text(product.HnQuantity);
            HnWeightGrossProperty.Value = Text(product.HnWeightGross);
            HnWeightNetProperty.Value = Text(product.HnWeightNet);
            HnLengthProperty.Value = Text(product.HnLength);
            HnHeightProperty.Value = Text(product.HnHeight);
            HnWidthProperty.Value = Text(product.HnWidth);
            HnVolumeProperty.Value = Text(product.HnVolume);
            HnStackLimitProperty.Value = Text(product.HnStackLimit);
            HnNestVolumeProperty.Value = Text(product.HnNestVolume);
            HnNestMaxProperty.Value = Text(product.HnNestMax);
            HnStackabilityProperty.Value = product.HnStackability ?? string.Empty;
            HnFragileProperty.Value = product.HnFragile ?? string.Empty;
            HnKeepUprightProperty.Value = product.HnKeepUpright ?? string.Empty;
        }

        private void ClearProductValues()
        {
            ArticleNameProperty.Value = string.Empty;
            ArticleNumberProperty.Value = string.Empty;
            DimensionsCheckedProperty.Value = string.Empty;

            SuPackagingProperty.Value = string.Empty;
            SuQuantityProperty.Value = string.Empty;
            SuWeightGrossProperty.Value = string.Empty;
            SuWeightNetProperty.Value = string.Empty;
            SuLengthProperty.Value = string.Empty;
            SuHeightProperty.Value = string.Empty;
            SuWidthProperty.Value = string.Empty;
            SuVolumeProperty.Value = string.Empty;
            SuStackLimitProperty.Value = string.Empty;
            SuNestVolumeProperty.Value = string.Empty;
            SuNestMaxProperty.Value = string.Empty;
            SuStackabilityProperty.Value = string.Empty;
            SuFragileProperty.Value = string.Empty;
            SuKeepUprightProperty.Value = string.Empty;

            PuPackagingProperty.Value = string.Empty;
            PuQuantityProperty.Value = string.Empty;
            PuWeightGrossProperty.Value = string.Empty;
            PuWeightNetProperty.Value = string.Empty;
            PuLengthProperty.Value = string.Empty;
            PuHeightProperty.Value = string.Empty;
            PuWidthProperty.Value = string.Empty;
            PuVolumeProperty.Value = string.Empty;
            PuStackLimitProperty.Value = string.Empty;
            PuNestVolumeProperty.Value = string.Empty;
            PuNestMaxProperty.Value = string.Empty;
            PuStackabilityProperty.Value = string.Empty;
            PuFragileProperty.Value = string.Empty;
            PuKeepUprightProperty.Value = string.Empty;

            HnPackagingProperty.Value = string.Empty;
            HnQuantityProperty.Value = string.Empty;
            HnWeightGrossProperty.Value = string.Empty;
            HnWeightNetProperty.Value = string.Empty;
            HnLengthProperty.Value = string.Empty;
            HnHeightProperty.Value = string.Empty;
            HnWidthProperty.Value = string.Empty;
            HnVolumeProperty.Value = string.Empty;
            HnStackLimitProperty.Value = string.Empty;
            HnNestVolumeProperty.Value = string.Empty;
            HnNestMaxProperty.Value = string.Empty;
            HnStackabilityProperty.Value = string.Empty;
            HnFragileProperty.Value = string.Empty;
            HnKeepUprightProperty.Value = string.Empty;
        }

        private static string Text(object? value)
        {
            return Convert.ToString(value, CultureInfo.CurrentCulture) ?? string.Empty;
        }

        private static string DateText(DateTime? value)
        {
            return value?.ToString("g", CultureInfo.CurrentCulture) ?? string.Empty;
        }
    }
}
