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.Navigation;
using System.Windows.Shapes;

using Foo.DataAccessLayer;
using Foo.DataAccessLayer.DataTypes;
using System.Windows.Threading;

namespace Foo.ClientServices.GUIWPForms
{
    /// <summary>
    /// Interaction logic for WorkspaceSelector_DeleteWorkspacePage.xaml
    /// </summary>
    public partial class WorkspaceSelector_DeleteWorkspacePage : Page
    {
        // passed to us by parent
        private WorkspaceSelector m_WorkspaceSelectorObj = null;
        private bool m_bPerformanceCache = false;

        // private state variables
        private string m_strWorkspaceName;

        public WorkspaceSelector_DeleteWorkspacePage(bool bPerformanceCache)
        {
            m_strWorkspaceName = String.Empty;
            m_bPerformanceCache = bPerformanceCache;
            if (!m_bPerformanceCache)
            {
            }
            InitializeComponent();
        }

        /// <summary>
        /// Occurs when page gets loaded
        /// </summary>        
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            if (!m_bPerformanceCache)
            {
                // Make sure that the Selector Frame is big enough to hold this page
                m_WorkspaceSelectorObj = (WorkspaceSelector)this.Tag;

                // Make sure that we have a workspace Name to work with otherwise Navigate Back
                if (String.IsNullOrEmpty(GUIState.LastSelectedWorkspaceName))
                    m_WorkspaceSelectorObj.NavigateToChildPage(ChildPages.WorkspaceSelector_StandardPage);
                else
                    m_strWorkspaceName = GUIState.LastSelectedWorkspaceName;

                // Set Height and Width
                m_WorkspaceSelectorObj.Height = this.Height + m_WorkspaceSelectorObj.MarginTopBottom;
                m_WorkspaceSelectorObj.Width = this.Width;   
             
                // Set the Warning Label for this Particular Workspace
                ConstructAreYouSureMessage();
            }
        }

        /// <summary>
        /// Constructs a thorough Warning Message to be displayed to the user in the label
        /// Iterates and warns the user of exactly the number of changes that will occur in the system
        /// </summary>
        private void ConstructAreYouSureMessage()
        {
            int nArtifactsCount = Data.Artifacts.GetArtifactLinkCountForWorkspace(m_strWorkspaceName);
            int nUniqueArtifactsCount = Data.Artifacts.GetUniqureArtifactsCountForWorkspace(m_strWorkspaceName);
            if (nArtifactsCount >= 0 && nUniqueArtifactsCount >= 0)
            {
                StringBuilder sr = new StringBuilder();
                sr.Append(String.Format("Are you Sure you want to Delete Workspace '{0}'? \n\n", m_strWorkspaceName));
                sr.Append(String.Format("This Workspace contains {0} Artifact Links ", (nArtifactsCount - nUniqueArtifactsCount)));
                sr.Append(String.Format("and {0} Artifacts. ", nUniqueArtifactsCount));
                sr.Append(String.Format("\n\n In Total: {0} Items will be deleted. There is no way to undo this operation.", nArtifactsCount));
                txtAreYouSure.Text = sr.ToString();
            }
            else
            {
                // an error occured
                Foo.Platform.ErrorReporting.UserError.Show("An Error Occurred", "Navigating you back to the WorkspaceSelector");
                m_WorkspaceSelectorObj.NavigateToChildPage(ChildPages.WorkspaceSelector_StandardPage);
            }
        }

        /// <summary>
        /// Button Yes Event Handler - Go ahead and delete the workspace
        /// </summary>        
        private void btnYes_Click(object sender, RoutedEventArgs e)
        {
            if (Data.Workspace.DeleteWorkspace(GUIState.LastSelectedWorkspaceName))
            {

                // Show Confirm Delete Message and Go Back
                StringBuilder sr = new StringBuilder();
                sr.Append(String.Format("The Workspace '{0}' was deleted successfully. \n\n", m_strWorkspaceName));
                sr.Append("You will no longer be able to access this Workspace and any Artifacts that were exclusive to it.");
                ShowMessageAndNavigateBack(sr.ToString());

                // Set GUI State
                GUIState.LastSelectedWorkspaceName = String.Empty;
            }
            else
            {
                // an error occured
                Foo.Platform.ErrorReporting.UserError.Show("An Error Occurred", String.Format("An Error occured while deleting the Workspace {0}. Navigating you back to the WorkspaceSelector", m_strWorkspaceName));
                m_WorkspaceSelectorObj.NavigateToChildPage(ChildPages.WorkspaceSelector_StandardPage);                
            }
        }

        /// <summary>
        /// Shows the passed in message in the text box and then navigates back to the main page
        /// </summary>
        /// <param name="strMessage"></param>
        private void ShowMessageAndNavigateBack(string strMessage)
        {
            // Hide the buttons
            btnYes.Visibility = Visibility.Hidden;
            btnNo.Visibility = Visibility.Hidden;

            // Show Message
            txtAreYouSure.Text = strMessage;

            // Pause for 2 second and Navigate Back
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1, 500);
            dispatcherTimer.Start();
        }

        /// <summary>
        /// Timer set specifically to allow the message to be seen for a little bit before navigating back
        /// </summary>        
        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            DispatcherTimer dispatcherTimer = (DispatcherTimer) sender;
            dispatcherTimer.Stop();

            // Navigate Back
            m_WorkspaceSelectorObj.NavigateToChildPage(ChildPages.WorkspaceSelector_StandardPage);            
        }

        /// <summary>
        /// Button No Event Handler - Go back to parent
        /// </summary>        
        private void btnNo_Click(object sender, RoutedEventArgs e)
        {
            m_WorkspaceSelectorObj.NavigateToChildPage(ChildPages.WorkspaceSelector_StandardPage);
        }
    }
}
