using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Internal.Framework.Controls;
using OliviaAddInPro.Helper;
using ArcGIS.Core.Geometry;

namespace OliviaAddInPro
{
    /// <summary>
    /// Interaction logic for PaneRecogidaSub1View.xaml
    /// </summary>
    public partial class PaneRecogidaSub1View : UserControl
    {
        public PaneRecogidaSub1View()
        {
            InitializeComponent();            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string capa=HelperGdb.OpenFileDialog(HelperGdb.TiposOpenFileDlg.OpenFtrClassPoint);
            if (!string.IsNullOrEmpty(capa))
            {
                label_capareco.Content = System.IO.Path.GetFileNameWithoutExtension(capa);
                if (DataContext is PaneRecogidaSub1ViewModel mod)
                {
                    if(!mod.AbiertaCapa(capa))
                        label_capareco.Content = Resource1.String_selec_capa;
                }
            }
            else
            {
                label_capareco.Content = Resource1.String_selec_capa;
                if (DataContext is PaneRecogidaSub1ViewModel mod)
                    mod.AbiertaCapa(null);
            }
        }


        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {

        }

        private void txtBox_kgCapac_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !PanelGlobal.IsValid(((TextBox)sender).Text + e.Text, 1, 10000);
        }

        private void comboBox_frac_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int i = -1;
            if (sender is System.Windows.Controls.ComboBox combo)
            {
                i = combo.SelectedIndex;

                if (i >= 0 && DataContext is PaneRecogidaSub1ViewModel mod)
                {
                    mod.ComboFracSel(i);
                    /*if (!mod.ComboFracSel(i))                        
                        combo.SelectedIndex = -1;*/
                }
            }

        }

        private void comboBox_tipoCarg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int i = -1;
            if (sender is System.Windows.Controls.ComboBox combo)
            {
                i = combo.SelectedIndex;
                
                if (i>=0 && DataContext is PaneRecogidaSub1ViewModel mod)
                {
                    mod.ComboCargSel(i);
                    /*if (!mod.ComboCargSel(i))
                        combo.SelectedIndex = -1;*/
                }
            }
        }

        private void comboBox_tipoVehic_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int i = -1;
            if (sender is System.Windows.Controls.ComboBox combo)
            {
                i = combo.SelectedIndex;

                if (i >= 0 && DataContext is PaneRecogidaSub1ViewModel mod)
                {
                    mod.ComboVehicSel(i);
                }
            }
        }

        private void comboBox_tipoCapac_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int i = -1;
            if (sender is System.Windows.Controls.ComboBox combo)
            {
                i = combo.SelectedIndex;

                if (i >= 0 && DataContext is PaneRecogidaSub1ViewModel mod)
                {
                    mod.ComboCapacSel(i);
                }
            }
        }

        private void txtBox_densCont_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !PanelGlobal.IsValid(((TextBox)sender).Text + e.Text, 0, 999);
        }

        private void comboBox_tipoLat_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int i = -1;
            if (sender is System.Windows.Controls.ComboBox combo)
            {
                i = combo.SelectedIndex;

                if (i >= 0 && DataContext is PaneRecogidaSub1ViewModel mod)
                {
                    mod.ComboLatSel(i);
                }
            }
        }

        private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !PanelGlobal.IsValid(((TextBox)sender).Text + e.Text, 0, 999);
        }

        private void button_capaplant_Click(object sender, RoutedEventArgs e)
        {
            string texto = "";

            ArcGIS.Core.Geometry.Geometry geomsal = HelperGdb.OpenGeom(HelperGdb.TiposOpenFileDlg.OpenFtrClassPoint,out texto);

            if ((DataContext is PaneRecogidaSub1ViewModel mod) && geomsal != null)
            {
                if (geomsal is MapPoint map)
                    mod.CoordsPlanta = map.Coordinate2D;
                else if (geomsal is Multipoint multip)
                {
                    if (multip.Copy2DCoordinatesToList().Count > 0)
                        mod.CoordsPlanta = multip.Copy2DCoordinatesToList().First();
                }
            }
            if (geomsal != null)
                label_capaplant.Content = texto;
            else
                label_capaplant.Content = Resource1.String_selec_capa;
        }
    }
}
