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 Yaulw.File;
using Yaulw.Assembly;
using Yaulw.Other;
using Yaulw.Thread;
using System.Timers;
//using Installables.All;

namespace MSLMobile
{

    /// <summary>
    /// Interaction logic for SetupMainWindow.xaml
    /// </summary>
    public partial class SetupMainWindow : Window
    {
        private Tail _tail = null;
        private bool _AllowWindowClosing = true;
        private TTimerDisp _dispTimer = null;
        private bool _bIsInstall = true;
        private string _RotateStates = @"-\|/-\|/-";
        private int _lastRotateState = 0;

        public SetupMainWindow()
        {
            InitializeComponent();
        }

        #region Private Methods

        private enum FooterColor
        {
            Blue,
            Red
        }

        /// <summary>
        /// Updates the nice pretty litte footer
        /// </summary>
        private void updatePrettyFooter_DoingWork(FooterColor color, string Content)
        {
            string[] periods = new string[] { ".", "..", "...", "....", "." };
            updatePrettyFooter(color, Content + " " + periods[_lastRotateState % 5] + " " + _RotateStates[_lastRotateState % 9], "McKesson Bridge Service");
            _lastRotateState++;
        }

        /// <summary>
        /// Updates the nice pretty litte footer
        /// </summary>
        private void updatePrettyFooter(FooterColor color, string Content)
        {
            updatePrettyFooter(color, Content, "McKesson Bridge Service");
        }

        /// <summary>
        /// Updates the nice pretty litte footer
        /// </summary>
        private void updatePrettyFooter(FooterColor color, string Content, string ToolTip)
        {
            // Set the Color            
            object[] param_s = null;
            if (color == FooterColor.Blue)
                param_s = new object[] { System.Windows.Media.Brushes.Blue };
            else
                param_s = new object[] { System.Windows.Media.Brushes.Red };
            Action<Brush> ab = delegate(Brush b) { lblFooter.Foreground = b; };
            Dispatcher.Invoke(ab, param_s);

            // Set the Content
            param_s = new object[] { Content };
            Action<string> a = delegate(string str) { lblFooter.Content = str; };
            Dispatcher.Invoke(a, param_s);

            // Set the ToolTip
            param_s = new object[] { ToolTip };
            a = delegate(string str) { lblFooter.ToolTip = str; };
            Dispatcher.Invoke(a, param_s);
        }

        /// <summary>
        /// Handles Incoming Data Stream from the Log File
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="newData"></param>
        /// <param name="bIsNewFile">True if this is a new Read, False Otherwise</param>
        private void _tail_IncomingData(object sender, string newData, bool bIsNewFile)
        {
            //if (bIsNewFile)
            //{
            //    object[] param_s = new object[] { newData };
            //    Action<string> a = delegate(string str) { textBoxLogViewer.Text = str; };
            //    Dispatcher.Invoke(a, param_s);
            //    Dispatcher.Invoke((DelegateCollection.Void_Func)textBoxLogViewer.ScrollToEnd, null);
            //}
            //else
            //{
            //    object[] param_s = new object[] { newData };
            //    Dispatcher.Invoke((DelegateCollection.Void_Param1_String_Func)textBoxLogViewer.AppendText, param_s);
            //    Dispatcher.Invoke((DelegateCollection.Void_Func)textBoxLogViewer.ScrollToEnd, null);
            //}
        }

        /// <summary>
        /// 
        /// </summary>        
        private void DispTimerEventHandler(object sender, ElapsedEventArgs e)
        {
            //// Check if Install/Uninstall Completed
            //bool bDone = false;
            ////if (_bIsInstall)
            ////    bDone = GenericInstall.s_PerformInstallCompleted;
            ////else
            ////    bDone = GenericInstall.s_PerformUninstallCompleted;

            //if (!bDone)
            //{
            //    updatePrettyFooter_DoingWork(FooterColor.Blue, _bIsInstall ? "Bridge Install In Progress" : "Bridge Uninstall In Progress");
            //}
            //else
            //{
            //    // Stop This Timer * Done Here *
            //    _dispTimer.Stop();

            //    bool bErrorOccured = false;
            //    //if (_bIsInstall)
            //    //    bErrorOccured = !GenericInstall.s_PerformInstallCompletedSuccessfully;
            //    //else
            //    //    bErrorOccured = !GenericInstall.s_PerformUninstallCompletedSuccessfully;

            //    if (!bErrorOccured)
            //        updatePrettyFooter(FooterColor.Blue, _bIsInstall ? "Bridge Install Completed Successfully" : "Bride Uninstall Completed Successfully");
            //    else
            //        updatePrettyFooter(FooterColor.Red, _bIsInstall ? "Bridge Install Completed with Error(s)" : "Bride Uninstall Completed with Error(s)");

            //    //if (_bIsInstall && Common.ServiceExists(Common_MediLytec.MediLytecPoundDef.BRIDGE_SERVICE_NAME))
            //    //{
            //    //    btnMainButton.Content = "Continue";
            //    //}
            //    //else
            //    //{
            //    //    _AllowWindowClosing = true;
            //    //    btnMainButton.Content = "Exit";
            //    //}
            //    btnMainButton.IsEnabled = true;
            //}
        }

        #endregion

        #region Window Event Handlers

        /// <summary>
        /// 
        /// </summary>        
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        //
        //    _tail = new Tail(Logger.APP_LOG_FILENAMEANDPATH);
        //    _tail.IncomingData += new Tail.IncomingDataHandler(_tail_IncomingData);
        //    _tail.StartMonitoring();

        //    if (!App.s_bIsUninstall)
        //    {
        //        _bIsInstall = true;
        //        btnMainButton.Content = "Continue";
        //        btnMainButton.IsEnabled = false;
        //        _AllowWindowClosing = false;
        //        //GenericInstall.PerformInstall();
        //    }
        //    else
        //    {
        //        _bIsInstall = false;
        //        btnMainButton.Content = "Exit";
        //        btnMainButton.IsEnabled = false;
        //        _AllowWindowClosing = true;
        //        //GenericInstall.PerformUninstall(App.s_strComponentsToUninstall);
        //    }

        //    // Start GUI Refresh Timer
        //    _dispTimer = new TTimerDisp(DispTimerEventHandler, 250, true);
        }

        /// <summary>
        /// 
        /// </summary>        
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //if (!_AllowWindowClosing)
            //    e.Cancel = true;
        }

        /// <summary>
        /// 
        /// </summary>        
        private void Window_Closed(object sender, EventArgs e)
        {
            //if (_tail != null)
            //{
            //    _tail.StopMonitoring(true);
            //    _tail = null;
            //}

            //Dispatcher.Invoke((DelegateCollection.Void_Func)textBoxLogViewer.Clear, null);

            // Let components know that Setup is now complete
            //if (btnMainButton.Content.ToString() != "Continue" && !_bIsInstall)
            //    GenericInstall.SetupMainCompleted();
        }

        /// <summary>
        /// Close Button - Event Handler
        /// </summary>     
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Main Button Event Handler
        /// </summary>        
        private void btnMainButton_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            if (btn.Content.ToString() == "Continue" && _bIsInstall)
            {
                // * NOT NEEDED * Delete Later
                //SetBridgeConfigWindow bridgeConfig = new SetBridgeConfigWindow();
                //bridgeConfig.Top = this.Top;
                //bridgeConfig.Left = this.Left;
                //bridgeConfig.Show();
                //bridgeConfig.Top = this.Top;
                //bridgeConfig.Left = this.Left;
                _AllowWindowClosing = true;
                this.Close();
            }
            else
            {
                this.Close();
            }
        }

        /// <summary>
        /// 
        /// </summary>        
        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }

        #endregion
    }
}
