using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FasdDesktopUi.Basics.Models;
using FasdDesktopUi.Pages.DetailsPage.Models;
using FasdDesktopUi.Basics;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Pages.DetailsPage.ViewModels
{
    public class DetailsPageViewModel : BaseViewModel
    {
        #region Properties

        private int shownValueColumnsCount;

        public cSupportCaseDataProvider DataProvider { get; set; }

        #region ZoomInPercent

        private int zoomInPercent;

        public int ZoomInPercent
        {
            get { return zoomInPercent; }
            set
            {
                if (value < 50)
                    value = 50;
                else if (value > 250)
                    value = 250;

                zoomInPercent = value;
                cFasdCockpitConfig.Instance.SetDetailsPageZoom(value);
                OnPropertyChanged();
            }
        }

        #endregion

        #region WidgetGeometry Property
        private List<DetailsPageWidgetGeometryModel> widgetGeometryList;
        public List<DetailsPageWidgetGeometryModel> WidgetGeometryList
        {
            get { return widgetGeometryList; }
            set
            {
                widgetGeometryList = value;
                OnPropertyChanged();
            }
        }

        #endregion

        #region Details Properties

        #region DetailsGeometry property

        private DetailsPageDataHistoryCollectionGeometryModel detailsGeometry;

        public DetailsPageDataHistoryCollectionGeometryModel DetailsGeometry
        {
            get { return detailsGeometry; }
            set
            {
                detailsGeometry = value;
                OnPropertyChanged();
            }
        }

        #endregion

        #region ShownValueColumnsCount
        public int ShownValueColumnsCount
        {
            get => shownValueColumnsCount; set
            {
                shownValueColumnsCount = value;
                OnPropertyChanged();
            }
        }
        #endregion

        #endregion

        #region Menu Properties

        #region MenuBarData property

        private List<cMenuDataBase> menuBarData;

        public List<cMenuDataBase> MenuBarData
        {
            get { return menuBarData; }
            set
            {
                menuBarData = value;
                OnPropertyChanged();
                OnPropertyChanged(nameof(MenuDrawerData));
            }
        }

        #endregion

        #region MenuDrawerData Property

        public List<cMenuDataBase> MenuDrawerData
        {
            get
            {
                if (MenuBarData != null)
                    return MenuBarData.Where(x => x.IconPositionIndex < 0).ToList();

                return new List<cMenuDataBase>();
            }
        }

        #endregion

        #endregion

        #endregion

        #region Methods

        public DetailsPageViewModel()
        {
            cFasdCockpitConfig.Instance.UiSettingsChanged += CockpitConfig_UiSettingsChanged;
        }

        private void CockpitConfig_UiSettingsChanged(object sender, EventArgs e)
        {
            ZoomInPercent = cFasdCockpitConfig.Instance.DetailsPageZoom;
        }

        #region Set DataContext

        public void SetGeometryValues(DetailsPageGeometry geometryValues)
        {
            var CM = MethodBase.GetCurrentMethod();
            LogMethodBegin(CM);
            try
            {
                WidgetGeometryList = geometryValues.WidgetGeometryList;
                DetailsGeometry = geometryValues.DetailsCollectionGeometry;
            }
            catch (Exception E)
            {
                LogEntry($"Passed geometry values: {geometryValues}");
                LogException(E);
            }
            finally
            {
                LogMethodEnd(CM);
            }
        }

        #endregion

        #endregion

    }
}