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 OliviaAddInPro.Helper;
using ArcGIS.Core.Data;
using System.Collections.ObjectModel;
using ArcGIS.Desktop.Internal.Framework.Controls;

namespace OliviaAddInPro.View
{
    /// <summary>
    /// Interaction logic for ProWndSelectFields.xaml
    /// </summary>
    public partial class ProWndSelectFields : ArcGIS.Desktop.Framework.Controls.ProWindow
    {
        private bool ok = false;
        public FeatureClass fc = null;
        public string err_st = string.Empty;

        public ProWndSelectFields(FeatureClass _fc, ShowProWndSelectFields proWndSelFlds)
        {
            InitializeComponent();
            fc = _fc;
            DataContext = proWndSelFlds;
            /*ObservableCollection<string> attribs_st = HelperGdb.GetFields(fc).Result;
            comboBoxFields.ItemsSource = attribs_st;
            listBoxAttributes.ItemsSource = attribs_st;*/

        }

        private void comboBoxFields_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int selindx = comboBoxFields.SelectedIndex;
            if (selindx >= 0)
            {
                string att = comboBoxFields.Items.GetItemAt(selindx).ToString();
                ObservableCollection<string> elems;
                elems = HelperGdb.GetFieldVals(fc, att, listBoxAttributes.SelectionMode == SelectionMode.Extended).Result;

                if (elems.Count == 0)
                {
                    if (HelperGdb.OutStr.Length > 0)
                        HelperGlobal.ponMsg(HelperGdb.OutStr, System.Windows.MessageBoxImage.Error);
                    else
                        HelperGlobal.ponMsg(Resource1.String_error_elems_tabla, System.Windows.MessageBoxImage.Warning);
                }
                listBoxAttributes.ItemsSource = elems;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ok = true;
            Close();
        }

        private void ProWindow_Closed(object sender, EventArgs e)
        {
            
        }

        private void ProWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (ok)
                DialogResult = true;
            else
                DialogResult = false;
        }

        private void listBoxAttributes_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ok = true;
            Close();
        }
    }
}
