using System.Windows;
using System.Windows.Controls;

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");
        }

        private void chk_autoexec_Checked(object sender, RoutedEventArgs e)
        {
            bkp.autoexec = true;
        }

        private void chk_autoexec_Unchecked(object sender, RoutedEventArgs e)
        {
            bkp.autoexec = false;
        }

        private void btn_setup_autorun_Click(object sender, RoutedEventArgs e)
        {
            string drive_name = methods.current_drive_label();
            methods.create_barryscan_script(drive_name);
            methods.add_scheduled_task(drive_name);
            MessageBox.Show("This computer has been configured to run barry when this drive is inserted");
        }
    }
}
