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 System.Windows.Interop;
using System.Windows.Threading;
using System.Threading;

using Foo.Platform;
using Foo.GUILib;
using Foo.DataAccessLayer;
using Foo.DataAccessLayer.DataTypes;
using System.Data;

namespace Foo.ClientServices.GUIWPForms
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class ArtifactWall : Window
    {
        private bool m_bPerformanceCache = false;

        // Height and Width Settings
        private int m_ItemHeight = 0;
        private int m_ItemWidth = 0;
        private int m_ImageHeight = 0;
        private int m_ImageWidth = 0;
        private int m_StackPanelHeight = 0;
        private int m_StackPanelWidth = 0;

        // Hard-coded Settings
        private const int ITEM_BORDER_THICKNESS = 2;
        private const int SELECTION_BORDER_SIZE_MARGIN = 10;


        // private DT holds all information needed for the DataGrid
        public DataTable ArtifactsDT { get; private set; }

        //object CreateImage(string Path)
        //{
        //    Image myImage1 = new Image();
        //    myImage1.Height = HEIGHT;
        //    myImage1.Width = WIDTH;
        //    BitmapImage myBitmapImage1 = new BitmapImage();
        //    myBitmapImage1.BeginInit();
        //    myBitmapImage1.UriSource = new Uri(Path);
        //    myBitmapImage1.DecodePixelHeight = HEIGHT;
        //    myBitmapImage1.DecodePixelWidth = WIDTH;            
        //    myBitmapImage1.EndInit();
        //    myImage1.Source = myBitmapImage1;

        //    //myImage1.Margin = new Thickness(25);
        //    return myImage1;
        //}

        Path CreatePath()
        {
            LineGeometry myLineGeometry = new LineGeometry();
            myLineGeometry.StartPoint = new Point(10, 50);
            myLineGeometry.EndPoint = new Point(1600,50);

            Path myPath = new Path();
            myPath.Stroke = Brushes.Black;
            myPath.StrokeThickness = 1;
            myPath.Data = myLineGeometry;
            return myPath;
        }

        void AddWorkspaceArtifacts(string workspaceName)
        {
            //using (OoganizerState oogyState = new OoganizerState())
            //{
            //    oogyState.SetCurrentWorkspace(workspaceName);

            //    using (OoganizerAPIProxy proxy = oogyState.GetAPIProxy())
            //    {
            //        Workspace workspace = proxy.API.GetWorkspace(workspaceName);

            //        foreach (OoganizerDAL.Artifact artifact in workspace.Artifacts)
            //        {
            //            xamCarouselPanel1.ChildElements.Add(CreateImage(artifact.Snapshot_Path));
            //        }
            //    }
            //}
        }

        private void CalculateHeightAndWidthSettings()
        {
            
            // Image Settings
            m_ImageHeight = 627;
            m_ImageWidth = 400;

            // StackPanel Settings
            const int STACK_PANEL_ITEMS_HEIGHT = 84;   // add together the other StackPanel items
            m_StackPanelHeight = m_ImageHeight + STACK_PANEL_ITEMS_HEIGHT;
            m_StackPanelWidth = m_ImageWidth;

            // Item settings
            m_ItemHeight = m_StackPanelHeight + SELECTION_BORDER_SIZE_MARGIN;
            m_ItemWidth = m_StackPanelWidth + SELECTION_BORDER_SIZE_MARGIN;            
        }

        /// <summary>
        /// 
        /// 
        /// </summary>
        private void UpdateArtifactsWall(SortOrderForWorkspaces sortOrder)
        {
            // First Step - Clear the DT
            ArtifactsDT.Rows.Clear();

            // Second Step - Calculate Height/Width Settings
            CalculateHeightAndWidthSettings();

            // Third Step - Fill the DT with Artifact Information
            ArtifactItem[] artifacts = Data.Artifacts.GetAllArtifactsInTheSystem(SortOrderForArtifacts.MostFrequentlyAccessedAscending);            
            foreach (ArtifactItem artifact in artifacts)
            {
                var row = ArtifactsDT.NewRow();
                ArtifactsDT.Rows.Add(row);

                // Border Settings
                row["BorderBrush"] = "Aqua";
                row["BorderThickness"] = ITEM_BORDER_THICKNESS;

                // StackPanel Settings
                row["StackPanelHeight"] = m_StackPanelHeight;
                row["StackPanelWidth"] = m_StackPanelWidth;
                row["StackPanelBackground"] = "Transparent";

                // Image Settings
                row["ImagePath"] = artifact.SnapshotFile;
                row["ImageHeight"] = m_ImageHeight;
                row["ImageWidth"] = m_ImageWidth;
                
                // Artifact Information                
                if(artifact.IsFile)
                    row["Title"] = artifact.Name + " (" + artifact.FileName + ")";
                else if(artifact.IsUrl)
                    row["Title"] = artifact.Name + " (" + artifact.Location + ")";

                row["LastLaunch"] = "Last Accessed: " + artifact.LastAccessed.ToString();
                row["FreqLaunch"] = "No. Times Accessed: " + artifact.AccessCounter.ToString();
                row["Notes"] = artifact.Note;    
            }

            // Third Step - Set xmlCarousel's Item Size
            xamCarouselPanel1.ViewSettings.ItemSize = new Size(m_ItemWidth, m_ItemHeight);

            // Fourth Step - Set the number of items * we could change this later*
            xamCarouselPanel1.ViewSettings.ItemsPerPage = 3;            

            // Fifth Step - Refresh The xmlCarousel                                    
            xamCarouselPanel1.ItemsSource = ArtifactsDT.DefaultView;                        
        }

        public ArtifactWall(bool bPerformanceCache)
        {
            InitializeComponent();

            // * For Performance Caching *
            if (WPFHiddenWindow.HiddenWindowConstructor(this, bPerformanceCache))
            {
                m_bPerformanceCache = true;
            }
            else
            {
                // Set Window Position Here
                WindowState = WindowState.Maximized;
                WindowStartupLocation = WindowStartupLocation.CenterScreen;

                xamCarouselPanel1.ViewSettings.ItemPath = CreatePath();
                xamCarouselPanel1.ViewSettings.ItemHorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
                xamCarouselPanel1.ViewSettings.ItemVerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
                xamCarouselPanel1.ViewSettings.AutoScaleItemContentsToFit = true;
                xamCarouselPanel1.Focus();
            }

            //WindowInteropHelper hInter = new WindowInteropHelper(this);
            //int width = System.Windows.Forms.Screen.FromHandle(hInter.Handle).Bounds.Width;
            //int height = System.Windows.Forms.Screen.FromHandle(hInter.Handle).Bounds.Height;
        }

        #region Page Events

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // * For Performance Caching *
            if (WPFHiddenWindow.HiddenWindowOnLoad(this, m_bPerformanceCache))
            {                
                // Do anything you would like to load
            }
            else
            {
                // InitializeArtifactsDT
                ArtifactsDT = new DataTable("ArtifactsData");

                // Border Related Settings
                ArtifactsDT.Columns.Add(new DataColumn("BorderBrush", typeof(string)));
                ArtifactsDT.Columns.Add(new DataColumn("BorderThickness", typeof(string)));

                // Image related Settings
                ArtifactsDT.Columns.Add(new DataColumn("ImagePath", typeof(string)));
                ArtifactsDT.Columns.Add(new DataColumn("ImageHeight", typeof(string)));
                ArtifactsDT.Columns.Add(new DataColumn("ImageWidth", typeof(string)));
                
                // StackPanel Settings
                ArtifactsDT.Columns.Add(new DataColumn("StackPanelHeight", typeof(string)));
                ArtifactsDT.Columns.Add(new DataColumn("StackPanelWidth", typeof(string)));
                ArtifactsDT.Columns.Add(new DataColumn("StackPanelBackground", typeof(string)));

                // Artifact Settings
                ArtifactsDT.Columns.Add(new DataColumn("Title", typeof(string)));
                ArtifactsDT.Columns.Add(new DataColumn("LastLaunch", typeof(string)));
                ArtifactsDT.Columns.Add(new DataColumn("FreqLaunch", typeof(string)));
                ArtifactsDT.Columns.Add(new DataColumn("Notes", typeof(string)));

                //              
                UpdateArtifactsWall(SortOrderForWorkspaces.Ascending);
            }            
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // * For Performance Caching *
            if (WPFHiddenWindow.HiddenWindowClosing(this, m_bPerformanceCache))
            {
                // Do anything you would like to load
            }
            else
            {

            }
        }

        #endregion

        # region Button Click Events

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnUp_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnDown_Click(object sender, RoutedEventArgs e)
        {

        }

        #endregion
    }
}

