//----------------------------------------------------------------------------
// AppConfig.cs
//
// Manages user preferences for the application (options, window state, etc).
//
// This is a part of the C1ReportDesigner package.
// Copyright (C) ComponentOne LLC
// All rights reserved.
//
// This source code is intended as a supplement to the
// C1ReportDesigner control and related documentation 
// provided with the product.
// See these sources for detailed information regarding the
// C1ReportDesigner product.
//----------------------------------------------------------------------------
using System;
using System.Xml;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Forms;

using C1.Win.C1ReportDesigner;
using ReportDesignerApp.Util;

namespace ReportDesignerApp
{
	/// <summary>
	/// AppConfig
	/// Save, load, and edit user preferences and application state
	/// </summary>
	public class AppConfig
	{
		private MainForm _owner;

        #region Constructors

        public AppConfig(MainForm owner)
		{
			_owner = owner;
        }

        #endregion

        #region Public

        // show the dialog
        public DialogResult ShowDialog()
        {
            using (OptionsForm dlg = new OptionsForm())
            {
                // get designer
                C1ReportDesigner designer = _owner.Designer;

                // initialize dialog with current preferences
                dlg.GridUnits = designer.Grid.Units;
                dlg.GridSpacing = designer.Grid.CustomSpacing;
                dlg.ShowGrid = designer.Grid.Visible;
                dlg.SnapToGrid = designer.Grid.Snap;
                dlg.GridColorMajor = designer.Grid.ColorMajor;
                dlg.GridColorMinor = designer.Grid.ColorMinor;
                dlg.FieldEdgeColor = designer.ColorFieldEdge;
                dlg.EnableUndo = designer.UndoStack.Enabled;
                dlg.ShowSubreportContent = designer.ShowSubreportContent; // <<B192>>
                dlg.DefaultExportFormat = _owner._defaultExportIndex;
                dlg.LoadOnStartup = _owner._loadOnStartUp;
                dlg.EmbedImages = _owner._embedImages;
                dlg.AutoSave = _owner._autoSave;
                dlg.ShowExportOptions = _owner._showExportOptions;
                dlg.CategorizeProperties = _owner.CategorizeProperties;
                dlg.SortReportList = _owner.SortReportList; // <<B239>>
                dlg.UseFieldWrappers = _owner.UseFieldWrappers;
                dlg.SyntaxCheck = _owner._syntaxCheck;
                dlg.SyntaxColoring = _owner._syntaxColoring;
                dlg.ScriptEditorFont = _owner._scriptEditorFont;

                // show dialog
                DialogResult dr = dlg.ShowDialog();
                if (dr != DialogResult.OK)
                    return dr;

                // apply changes to app
                designer.Grid.Units = dlg.GridUnits;
                designer.Grid.CustomSpacing = dlg.GridSpacing;
                designer.Grid.Visible = dlg.ShowGrid;
                designer.Grid.Snap = dlg.SnapToGrid;
                designer.Grid.ColorMajor = dlg.GridColorMajor;
                designer.Grid.ColorMinor = dlg.GridColorMinor;
                designer.ColorFieldEdge = dlg.FieldEdgeColor;
                designer.UndoStack.Enabled = dlg.EnableUndo;
                designer.ShowSubreportContent = dlg.ShowSubreportContent; // <<B192>>
                _owner._defaultExportIndex = dlg.DefaultExportFormat;
                _owner._loadOnStartUp = dlg.LoadOnStartup;
                _owner._embedImages = dlg.EmbedImages;
                _owner._autoSave = dlg.AutoSave;
                _owner._showExportOptions = dlg.ShowExportOptions;
                _owner.CategorizeProperties = dlg.CategorizeProperties;
                _owner.UseFieldWrappers = dlg.UseFieldWrappers;
                _owner._syntaxCheck = dlg.SyntaxCheck;			// <<B193>>
                _owner._syntaxColoring = dlg.SyntaxColoring;		// <<B193>>
                _owner._scriptEditorFont = dlg.ScriptEditorFont;		// <<B193>>
                _owner.SortReportList = dlg.SortReportList;       // <<B239>>

                // save changes
                SaveAppState();

                // done
                return dr;
            }
        }

		// save application state into registry
        public bool SaveAppState()
        {
            try
            {
                UserPreferences prefs = new UserPreferences(_owner);
                AppSettings.SaveObject("preferences", prefs);
                return true;
            }
            catch { }
            return false;
        }

		// restore application state from registry
        public bool LoadAppState()
        {
            try
            {
                UserPreferences prefs = new UserPreferences(_owner);
                AppSettings.LoadObject("preferences", prefs);
            }
            catch { }
            return false;
        }

        #endregion
    }

    internal class FormState
    {
        private Form _owner;

        #region Constructors
        public FormState(Form owner)
        {
            _owner = owner;
        }
        #endregion

        #region Internal properties
        internal Form Owner
        {
            get { return _owner; }
        }
        #endregion

        #region Public properties
        // window state preferences
        [DefaultValue(FormWindowState.Normal)]
        public FormWindowState WindowState
        {
            get { return _owner.WindowState; }
            set { _owner.WindowState = value; }
        }

        [DefaultValue(typeof(Rectangle), "0,0,0,0")]
        public Rectangle WindowPosition
        {
            get
            {
                return (_owner.WindowState == FormWindowState.Normal)
                    ? _owner.Bounds
                    : Rectangle.Empty;
            }
            set
            {
                if (_owner.WindowState == FormWindowState.Normal && value.Width > 0 && value.Height > 0)
                    _owner.Bounds = value;
            }
        }
        #endregion
    }

    /// <summary>
	/// 
	/// </summary>
	internal class UserPreferences
	{
		private MainForm _owner;
		private C1ReportDesigner _designer;

        #region Constructors
        internal UserPreferences(MainForm owner)
		{
			_owner = owner;
			_designer = owner.Designer;
        }
        #endregion

        #region Public properties
        // designer preferences
		[DefaultValue(true)]
		public bool GridVisible
		{
			get { return _designer.Grid.Visible; }
			set { _designer.Grid.Visible = value; }
		}

		[DefaultValue(false)]
		public bool GridSnap
		{
			get { return _designer.Grid.Snap; }
			set { _designer.Grid.Snap = value; }
		}

        [DefaultValue(GridUnitsEnum.Auto)]
        public GridUnitsEnum GridUnits
        {
            get { return _designer.Grid.Units; }
            set { _designer.Grid.Units = value; }
        }
        
        [DefaultValue(1440)]
        public int GridSpacing
        {
            get { return _designer.Grid.CustomSpacing; }
            set { _designer.Grid.CustomSpacing = value; }
        }

        [DefaultValue(typeof(Color), "Black")]
        public Color GridColorMajor
        {
            get { return _designer.Grid.ColorMajor; }
            set { _designer.Grid.ColorMajor = value; }
        }

        [DefaultValue(typeof(Color), "DimGray")]
        public Color GridColorMinor
        {
            get { return _designer.Grid.ColorMinor; }
            set { _designer.Grid.ColorMinor = value; }
        }

        [DefaultValue(typeof(Color), "Control")]
        public Color FieldEdgeColor
        {
            get { return _designer.ColorFieldEdge; }
            set { _designer.ColorFieldEdge = value; }
        }
        
        [DefaultValue(true)]
		public bool EnableUndo
		{
			get { return _designer.UndoStack.Enabled; }
			set { _designer.UndoStack.Enabled = value; }
		}
		
        [DefaultValue(true)]
		public bool ShowSubreportContent
		{
			get { return _designer.ShowSubreportContent; }
			set { _designer.ShowSubreportContent = value; }
		}
        
        [DefaultValue("")]
        public string DefaultStyleName
        {
            get { return _owner._defaultStyleName; }
            set { _owner._defaultStyleName = value; }
        }

		// main preferences
		[DefaultValue(2)] // Drilldown (index into filter is 1-based)
		public int  DefaultExportIndex
		{
			get { return _owner._defaultExportIndex; }
			set { _owner._defaultExportIndex = value; }
		}
		
        [DefaultValue(true)]
		public bool LoadOnStartUp
		{
			get { return _owner._loadOnStartUp; }
			set { _owner._loadOnStartUp = value; }
		}
		
        [DefaultValue(true)]
		public bool EmbedImages
		{
			get { return _owner._embedImages; }
			set { _owner._embedImages = value; }
		}
		
        [DefaultValue(false)]
		public bool AutoSave
		{
			get { return _owner._autoSave; }
			set { _owner._autoSave = value; }
		}

        [DefaultValue(true)]
        public bool ShowExportOptions
        {
            get { return _owner._showExportOptions; }
            set { _owner._showExportOptions = value; }
        }
		
        [DefaultValue(true)]
		public bool CategorizeProperties
		{
			get { return _owner.CategorizeProperties; }
			set { _owner.CategorizeProperties = value; }
		}
		
        [DefaultValue(false)]
		public bool UseFieldWrappers
		{
			get { return _owner.UseFieldWrappers; }
			set { _owner.UseFieldWrappers = value; }
		}

		// window state preferences
		[DefaultValue(FormWindowState.Normal)]
		public FormWindowState WindowState
		{
			get { return _owner.WindowState; }
			set { _owner.WindowState = value; }
		}
		
        [DefaultValue(typeof(Rectangle), "0,0,0,0")]
		public Rectangle WindowPosition
		{
			get 
			{
				return (_owner.WindowState == FormWindowState.Normal)
					? _owner.Bounds
					: Rectangle.Empty;
			}
			set 
			{
				if (_owner.WindowState == FormWindowState.Normal && value.Width > 0 && value.Height > 0)
					_owner.Bounds = value;
			}
		}

		// script editor preferences
		[DefaultValue(true)]
		public bool AutoSyntaxCheck
		{
			get { return _owner._syntaxCheck; }
			set { _owner._syntaxCheck = value; }
		}
		
        [DefaultValue(true)]
		public bool SyntaxColoring
		{
			get { return _owner._syntaxColoring; }
			set { _owner._syntaxColoring = value; }
		}
		
        [DefaultValue(typeof(Font), "Courier New, 9pt")]
		public Font ScriptEditorFont
		{
			get { return _owner._scriptEditorFont; }
			set { _owner._scriptEditorFont = value; }
        }

        public string DockPanelsState
        {
            get
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    C1.Win.C1Command.C1DockingTab.SaveLayout(_owner, ms);
                    return System.Convert.ToBase64String(ms.ToArray());
                }
            }
            set
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] data = System.Convert.FromBase64String(value);
                    ms.Write(data, 0, data.Length);
                    ms.Seek(0, SeekOrigin.Begin);
                    C1.Win.C1Command.C1DockingTab.RestoreLayout(_owner, ms);
                }
            }
        }

        public bool QatBelowRibbon
        {
            get { return _owner.Ribbon.Qat.BelowRibbon; }
            set { _owner.Ribbon.Qat.BelowRibbon = value; }
        }

        public string QatItems
        {
            get { return _owner.Ribbon.QatItemIDs; }
            set { _owner.Ribbon.QatItemIDs = value; }
        }
        #endregion
    }
}
