using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Wpf_Demo.ViewModels;

namespace Wpf_Demo
{
    /// <summary>
    /// Interaction logic for DetailsWindow.xaml
    /// </summary>
    public partial class DetailsWindow : Window
    {
        public DetailsWindow()
        {
            InitializeComponent();
        }

        public void ShowDialog(GoalViewModel currentItem)
        {
            adaptUI(currentItem);
            this.ShowDialog();
        }

        private void ForceBinding()
        {
            try
            {
                this.CurrentItem.Done = chkDone.IsChecked.Value;
                this.CurrentItem.Goal = txtGoal.Text;
                this.CurrentItem.PriorityInt = cmbPriority.SelectedIndex;
                this.CurrentItem.Cost = decimal.Parse(txtCost.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
            }
        }

        private void adaptUI(GoalViewModel goal)
        {
            if (goal == null)
                return;

            if (goal.IsNew) // Add new index.
            {
                this.ListTitle.Text = "Add Bucket";
                this.ContentPanel.DataContext = goal;
            }
            else
            {
                this.ListTitle.Text = "Edit Bucket";
                this.ContentPanel.DataContext = goal;
                LastItemVersion = goal.Clone();
            }
        }

        public bool IsAdding
        {
            get
            {
                return CurrentItem.IsNew;
            }
        }

        public GoalViewModel LastItemVersion { get; private set; }

        public GoalViewModel CurrentItem
        {
            get
            {
                return (GoalViewModel)this.ContentPanel.DataContext;
            }
        }

        private void Ok_Click(object sender, RoutedEventArgs e)
        {
            ForceBinding();
            // Validating Items.
            if (!CurrentItem.IsValid)
            {
                MessageBox.Show(CurrentItem.ValidationMessage, "Error", MessageBoxButton.OK);
                txtGoal.Focus();
                return;
            }
            // Saving
            CurrentItem.Save();
            App.ViewModel.SortList();
            this.Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            if (!IsAdding)
            {
                CurrentItem.Cost = LastItemVersion.Cost;
                CurrentItem.Done = LastItemVersion.Done;
                CurrentItem.Goal = LastItemVersion.Goal;
                CurrentItem.Priority = LastItemVersion.Priority;
            }
            this.Close();
        }

        private void txtCost_GotFocus(object sender, RoutedEventArgs e)
        {
            txtCost.SelectAll();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtGoal.Focus();
        }
    }
}
