using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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.Shapes;
using Microsoft.WindowsAPICodePack.Dialogs;
using System.IO;

namespace barry
{
    /// <summary>
    /// Logique d'interaction pour OptionsWindow.xaml
    /// </summary>
    public partial class OptionsWindow : Window
    {
        Methods methods = new Methods();
        BackupConfig bkp;
        bool _loaded = false;
        public OptionsWindow(BackupConfig current_bkp)
        {

            InitializeComponent();
            _loaded = true;

            bkp = current_bkp;

            lbl_drive.Content = methods.current_drive_letter();
            txt_path.Text = bkp.dir_path;
            txt_subdir.Text = bkp.local_subdir;
            for (int i = 0; i < cb_bkptype.Items.Count; i++)
            {
                if (int.Parse(((ComboBoxItem)cb_bkptype.Items[i]).Tag.ToString()) == bkp.bkp_type)
                {
                    cb_bkptype.SelectedIndex = i;
                    break;
                }
            }
        }

        private void btn_save_Click(object sender, RoutedEventArgs e)
        {
            string diag = bkp.checkup();
            if (diag.Length > 0)
            {
                MessageBox.Show(diag, "Check-Up", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            methods.save_this_cpu(bkp);
            Close();
        }

        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void cb_bkptype_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!_loaded) { return; }
            bkp.bkp_type = int.Parse(((ComboBoxItem)cb_bkptype.SelectedItem).Tag.ToString());
            update_window();
        }

        private void update_window()
        {
            if (!_loaded) { return; }
            lbl_decription.Content = bkp.describe();
        }

        private void txt_subdir_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!_loaded) { return; }
            //if (! System.IO.Directory.Exists(txt_subdir.Text))
            //{
            //    lbl_decription.Content = "Wait! Barry does not find this directory on the computer!";
            //    return;
            //}
            
            bkp.local_subdir = txt_subdir.Text;
            update_window();
        }

        private void txt_path_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!_loaded) { return; }
            bkp.dir_path = txt_path.Text;
            update_window();
        }

        private void btn_explorecpu_Click(object sender, RoutedEventArgs e)
        {
            txt_path.Text = methods.select_directory_dialog("Select a directory on your computer");
        }

        private void btn_exploredevice_Click(object sender, RoutedEventArgs e)
        {
            txt_subdir.Text = methods.select_directory_dialog("Select a directory on your device");
        }


    }
}
