using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Controls;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;
using OliviaAddInPro.Model;
using OliviaAddInPro.View;
using OliviaAddInPro.Helper;
using OliviaAddInPro.Services;

namespace OliviaAddInPro
{
    internal class DockpaneConfigViewModel : DockPane
    {
        private bool firstTimeShow = true;
        private static bool hideTemp = false;
        private const string _dockPaneID = "OliviaAddInPro_DockpaneConfig";
        PaneConfigViewModel paneConfig;
        protected DockpaneConfigViewModel() 
        {
            paneConfig = new PaneConfigViewModel();
            _currentPage = (PanelViewModelBase) paneConfig;

        }

        /// <summary>
        /// Show the DockPane.
        /// </summary>
        internal static void Show()
        {
            DockPane pane = FrameworkApplication.DockPaneManager.Find(_dockPaneID);
            if (pane == null)
                return;

            pane.Activate();
        }
        protected override void OnHidden()
        {
            OliviaGlob.ViewSetFlagTipEjec(TiposEjecucion.Ninguno);
        }
        internal static void Hide_()
        {
            DockPane pane = FrameworkApplication.DockPaneManager.Find(_dockPaneID);
            if (pane == null)
                return;
            hideTemp = true;
            pane.Hide();
        }
        internal static void Reset()
        {
            //HAY QUE HACER QUE SE LIMPIE TODO
        }

        //The parameter passed to this method will be true if the Dockpane is being opened and it is false when you close the dockpane
        //also false the first time
        protected override void OnShow(bool isVisible)
        {
            if (isVisible == false && !firstTimeShow)
            {
                //avisa de cerrar la ventana
                //OliviaGlob.SetFlagTipEjec(TiposEjecucion.Ninguno);
                if (paneConfig!=null && paneConfig.hay_cambios)
                {
                    var guardar = HelperGlobal.ponMsg("¿Desea guardar los cambios antes de salir?", System.Windows.MessageBoxImage.Information, "OLIVIA", System.Windows.MessageBoxButton.YesNo);
                    if (guardar)
                    {
                        Respuesta<bool> resp = ConfigServ.Serv.Guardar(paneConfig.Configuracion);
                        if (!resp.Value)
                            HelperGlobal.ponMsg(resp.Error.First());
                    }
                    paneConfig.hay_cambios = false;
                }
            }
            if (firstTimeShow)
                firstTimeShow = false;
        }

        /// <summary>
        /// Text shown near the top of the DockPane.
        /// </summary>
        private string _heading = Resource1.String_header_Config;
        public string Heading
        {
            get { return _heading; }
            set
            {
                SetProperty(ref _heading, value, () => Heading);
            }
        }

        private PanelViewModelBase _currentPage;
        public PanelViewModelBase CurrentPage
        {
            get { return _currentPage; }
            set
            {
                SetProperty(ref _currentPage, value, () => CurrentPage);
            }
        }
    }
}
