//----------------------------------------------------------------------------
// GroupEditorForm.cs
//
// Group editor dialog for use with C1ReportDesigner app
// Allows the user to create, remove, edit, and reorder
// report groups.
//
// This is a part of the C1ReportDesigner package.
// Copyright (C) GrapeCity, Inc
// 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.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using C1.C1Report;
using C1.Win.Localization;

namespace ReportDesignerApp.Util
{
	/// <summary>
	/// Summary description for GroupEditorForm.
	/// </summary>
	public class GroupEditorForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button _btnCancel;
		private System.Windows.Forms.Button _btnOK;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.ListBox _lstGroups;
		private System.Windows.Forms.CheckBox _chkHeader;
		private System.Windows.Forms.CheckBox _chkFooter;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.ComboBox _cmbSort;
		private System.Windows.Forms.ComboBox _cmbKeep;
		private System.Windows.Forms.TextBox _txtName;
		private System.Windows.Forms.Button _btnAdd;
		private System.Windows.Forms.Button _btnDelete;
		private System.Windows.Forms.Button _btnUp;
		private System.Windows.Forms.Button _btnDown;
		private System.Windows.Forms.ComboBox _cmbGroupBy;
		private System.Windows.Forms.GroupBox groupBox1;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public GroupEditorForm()
		{
			InitializeComponent();


            //
            this._cmbSort.Items.Clear();
            this._cmbSort.Items.Add(Strings.GroupEditorForm.SortNone);
            this._cmbSort.Items.Add(Strings.GroupEditorForm.SortAscending);
            this._cmbSort.Items.Add(Strings.GroupEditorForm.SortDescending);

            //
            this._cmbKeep.Items.Clear();
            this._cmbKeep.Items.Add(Strings.GroupEditorForm.KeepNo);
            this._cmbKeep.Items.Add(Strings.GroupEditorForm.KeepWholeGroup);
            this._cmbKeep.Items.Add(Strings.GroupEditorForm.KeepFirstDetail);

            //
            AutoScaleMode = AutoScaleMode.None;
            Font = MainForm.DefaultAppFont;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(GroupEditorForm));
            this._btnCancel = new System.Windows.Forms.Button();
            this._btnOK = new System.Windows.Forms.Button();
            this._lstGroups = new System.Windows.Forms.ListBox();
            this._btnAdd = new System.Windows.Forms.Button();
            this._btnDelete = new System.Windows.Forms.Button();
            this._chkHeader = new System.Windows.Forms.CheckBox();
            this._chkFooter = new System.Windows.Forms.CheckBox();
            this._cmbSort = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this._cmbKeep = new System.Windows.Forms.ComboBox();
            this.label4 = new System.Windows.Forms.Label();
            this._txtName = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this._btnUp = new System.Windows.Forms.Button();
            this._btnDown = new System.Windows.Forms.Button();
            this._cmbGroupBy = new System.Windows.Forms.ComboBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.SuspendLayout();
            // 
            // _btnCancel
            // 
            this._btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this._btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this._btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this._btnCancel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this._btnCancel.Location = new System.Drawing.Point(341, 272);
            this._btnCancel.Name = "_btnCancel";
            this._btnCancel.Size = new System.Drawing.Size(75, 23);
            this._btnCancel.TabIndex = 17;
            this._btnCancel.Text = "&Cancel";
            // 
            // _btnOK
            // 
            this._btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this._btnOK.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this._btnOK.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this._btnOK.Location = new System.Drawing.Point(260, 272);
            this._btnOK.Name = "_btnOK";
            this._btnOK.Size = new System.Drawing.Size(75, 23);
            this._btnOK.TabIndex = 16;
            this._btnOK.Text = "&OK";
            this._btnOK.Click += new System.EventHandler(this._btnOK_Click);
            // 
            // _lstGroups
            // 
            this._lstGroups.IntegralHeight = false;
            this._lstGroups.Location = new System.Drawing.Point(8, 24);
            this._lstGroups.Name = "_lstGroups";
            this._lstGroups.Size = new System.Drawing.Size(168, 192);
            this._lstGroups.TabIndex = 1;
            this._lstGroups.SelectedIndexChanged += new System.EventHandler(this._lstGroups_SelectedIndexChanged);
            // 
            // _btnAdd
            // 
            this._btnAdd.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this._btnAdd.Location = new System.Drawing.Point(20, 222);
            this._btnAdd.Name = "_btnAdd";
            this._btnAdd.Size = new System.Drawing.Size(75, 23);
            this._btnAdd.TabIndex = 2;
            this._btnAdd.Text = "&Add";
            this._btnAdd.Click += new System.EventHandler(this._btnAdd_Click);
            // 
            // _btnDelete
            // 
            this._btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this._btnDelete.Location = new System.Drawing.Point(101, 222);
            this._btnDelete.Name = "_btnDelete";
            this._btnDelete.Size = new System.Drawing.Size(75, 23);
            this._btnDelete.TabIndex = 3;
            this._btnDelete.Text = "&Delete";
            this._btnDelete.Click += new System.EventHandler(this._btnDelete_Click);
            // 
            // _chkHeader
            // 
            this._chkHeader.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this._chkHeader.Location = new System.Drawing.Point(240, 200);
            this._chkHeader.Name = "_chkHeader";
            this._chkHeader.Size = new System.Drawing.Size(176, 21);
            this._chkHeader.TabIndex = 14;
            this._chkHeader.Text = "Header Section";
            this._chkHeader.CheckedChanged += new System.EventHandler(this.controls_Changed);
            // 
            // _chkFooter
            // 
            this._chkFooter.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this._chkFooter.Location = new System.Drawing.Point(240, 224);
            this._chkFooter.Name = "_chkFooter";
            this._chkFooter.Size = new System.Drawing.Size(176, 21);
            this._chkFooter.TabIndex = 15;
            this._chkFooter.Text = "Footer Section";
            this._chkFooter.CheckedChanged += new System.EventHandler(this.controls_Changed);
            // 
            // _cmbSort
            // 
            this._cmbSort.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this._cmbSort.Location = new System.Drawing.Point(240, 120);
            this._cmbSort.Name = "_cmbSort";
            this._cmbSort.Size = new System.Drawing.Size(176, 21);
            this._cmbSort.TabIndex = 11;
            this._cmbSort.SelectedIndexChanged += new System.EventHandler(this.controls_Changed);
            // 
            // label1
            // 
            this.label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.label1.Location = new System.Drawing.Point(224, 56);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(144, 16);
            this.label1.TabIndex = 8;
            this.label1.Text = "Group By";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // label2
            // 
            this.label2.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.label2.Location = new System.Drawing.Point(224, 104);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(144, 16);
            this.label2.TabIndex = 10;
            this.label2.Text = "Sort";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // label3
            // 
            this.label3.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.label3.Location = new System.Drawing.Point(224, 152);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(144, 16);
            this.label3.TabIndex = 12;
            this.label3.Text = "Keep Together";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // _cmbKeep
            // 
            this._cmbKeep.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this._cmbKeep.Location = new System.Drawing.Point(240, 168);
            this._cmbKeep.Name = "_cmbKeep";
            this._cmbKeep.Size = new System.Drawing.Size(176, 21);
            this._cmbKeep.TabIndex = 13;
            this._cmbKeep.SelectedIndexChanged += new System.EventHandler(this.controls_Changed);
            // 
            // label4
            // 
            this.label4.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.label4.Location = new System.Drawing.Point(8, 8);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(144, 16);
            this.label4.TabIndex = 0;
            this.label4.Text = "Groups";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // _txtName
            // 
            this._txtName.Location = new System.Drawing.Point(240, 24);
            this._txtName.Name = "_txtName";
            this._txtName.Size = new System.Drawing.Size(176, 20);
            this._txtName.TabIndex = 7;
            this._txtName.TextChanged += new System.EventHandler(this.controls_Changed);
            this._txtName.Validating += new System.ComponentModel.CancelEventHandler(this._txtName_Validating);
            // 
            // label5
            // 
            this.label5.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.label5.Location = new System.Drawing.Point(224, 8);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(144, 16);
            this.label5.TabIndex = 6;
            this.label5.Text = "Name";
            this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // _btnUp
            // 
            this._btnUp.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this._btnUp.Font = new System.Drawing.Font("Wingdings", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(2)));
            this._btnUp.Location = new System.Drawing.Point(184, 24);
            this._btnUp.Name = "_btnUp";
            this._btnUp.Size = new System.Drawing.Size(24, 24);
            this._btnUp.TabIndex = 4;
            this._btnUp.Text = "y";
            this._btnUp.Click += new System.EventHandler(this._btnUp_Click);
            // 
            // _btnDown
            // 
            this._btnDown.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this._btnDown.Font = new System.Drawing.Font("Wingdings", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(2)));
            this._btnDown.Location = new System.Drawing.Point(184, 56);
            this._btnDown.Name = "_btnDown";
            this._btnDown.Size = new System.Drawing.Size(24, 24);
            this._btnDown.TabIndex = 5;
            this._btnDown.Text = "y";
            this._btnDown.Click += new System.EventHandler(this._btnDown_Click);
            // 
            // _cmbGroupBy
            // 
            this._cmbGroupBy.Location = new System.Drawing.Point(240, 72);
            this._cmbGroupBy.Name = "_cmbGroupBy";
            this._cmbGroupBy.Size = new System.Drawing.Size(176, 21);
            this._cmbGroupBy.TabIndex = 9;
            this._cmbGroupBy.Validating += new System.ComponentModel.CancelEventHandler(this._cmbGroupBy_Validating);
            this._cmbGroupBy.TextChanged += new System.EventHandler(this.controls_Changed);
            // 
            // groupBox1
            // 
            this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.groupBox1.Location = new System.Drawing.Point(8, 264);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(416, 2);
            this.groupBox1.TabIndex = 18;
            this.groupBox1.TabStop = false;
            // 
            // GroupEditorForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this._btnCancel;
            this.ClientSize = new System.Drawing.Size(426, 303);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this._btnUp);
            this.Controls.Add(this.label1);
            this.Controls.Add(this._cmbSort);
            this.Controls.Add(this._chkHeader);
            this.Controls.Add(this._btnAdd);
            this.Controls.Add(this._lstGroups);
            this.Controls.Add(this._btnOK);
            this.Controls.Add(this._btnCancel);
            this.Controls.Add(this._btnDelete);
            this.Controls.Add(this._chkFooter);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this._cmbKeep);
            this.Controls.Add(this.label4);
            this.Controls.Add(this._txtName);
            this.Controls.Add(this.label5);
            this.Controls.Add(this._btnDown);
            this.Controls.Add(this._cmbGroupBy);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "GroupEditorForm";
            this.Padding = new System.Windows.Forms.Padding(1);
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Sorting and Grouping";
            this.Load += new System.EventHandler(this.GroupEditorForm_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		// private fields
		private C1Report _report;
		private C1Report _edit;
		private bool _updating = false;

		// bind dialog to a report and show it
		public DialogResult ShowDialog(C1Report report)
		{
			// create a copy of the report, this is the one we'll edit with the dialog
			_report = report;
			_edit = new C1Report();
			_edit.CopyFrom(_report);

			// set text on graphical buttons
			_btnUp.Text   = ((char)0xe9).ToString();
			_btnDown.Text = ((char)0xea).ToString();

			// populate list of field to group by
			_cmbGroupBy.Sorted = true;
			string[] fields = _edit.DataSource.GetDBFieldList(true);
            if (fields != null)
			    foreach (string field in fields)
				    _cmbGroupBy.Items.Add(field);

			// ready to go
			return base.ShowDialog();
		}

		// save results and dismiss dialog
		private void _btnOK_Click(object sender, System.EventArgs e)
		{
			// apply changes
			_report.CopyFrom(_edit);

			// done
			DialogResult = DialogResult.OK;
		}

		// populate list and edit controls
		private void GroupEditorForm_Load(object sender, System.EventArgs e)
		{
			PopulateList(0);
		}

		// populate group list, initialize edit controls
		private void PopulateList(int index)
		{
			// populate list
			_lstGroups.Items.Clear();
			foreach (Group g in _edit.Groups)
				_lstGroups.Items.Add(new GroupItem(g));

			// select first item
			int cnt = _lstGroups.Items.Count;
			if (index > cnt-1) index = cnt-1;
			if (index > -1 && index < cnt)
				_lstGroups.SelectedIndex = index;

			// update UI
			UpdateUI();
		}

		// update controls with group info
		private void _lstGroups_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// get selected group			
			Group g = ((GroupItem)_lstGroups.SelectedItem)._group;

			// update form info
			_updating = true;
			_txtName.Text			= g.Name;
			_cmbGroupBy.Text		= g.GroupBy;
			_cmbSort.SelectedIndex	= (int)g.Sort;
			_cmbKeep.SelectedIndex	= (int)g.KeepTogether;
			_chkHeader.Checked		= g.SectionHeader.Visible;
			_chkFooter.Checked		= g.SectionFooter.Visible;
			_updating = false;

			// update UI
			UpdateUI();
		}
		private void UpdateUI()
		{
			// no group selected? disable editors
			bool enable = (_lstGroups.SelectedItem != null);
			_txtName.Enabled    = enable;
			_cmbGroupBy.Enabled = enable;
			_cmbSort.Enabled    = enable;
			_cmbKeep.Enabled    = enable;
			_chkHeader.Enabled  = enable;
			_chkFooter.Enabled  = enable;
			_btnDelete.Enabled  = enable;

			// udpate up/down buttons
			int cnt = _lstGroups.Items.Count;
			_btnUp.Enabled	 = _lstGroups.SelectedIndex > 0;
			_btnDown.Enabled = (cnt > 0 && _lstGroups.SelectedIndex < cnt-1);
		}

		// update group with control info
		private void controls_Changed(object sender, System.EventArgs e)
		{
			// not while we're updating the controls
			if (_updating) return;

			// get selected group			
			Group g = ((GroupItem)_lstGroups.SelectedItem)._group;

			Section s = g.SectionHeader;
			if (s.Name == g.Name + "_Header")
			{
				s.Name = string.Empty;
			}
			s = g.SectionFooter;
			if (s.Name == g.Name + "_Footer")
			{
				s.Name = string.Empty;
			}

			// update object info
			g.Name = _txtName.Text;
			g.GroupBy = C1.C1Report.Util.Misc.Bracket(_cmbGroupBy.Text);
			g.Sort = (SortEnum)_cmbSort.SelectedIndex;
			g.KeepTogether = (KeepTogetherEnum)_cmbKeep.SelectedIndex;
			
			// make sure group has a name
			if (g.Name.Length == 0)
				g.Name = Strings.GroupEditorForm.NewGroupName;

            // update group header section (no spaces in default name)
            s = g.SectionHeader;
			if (s.Name.Length == 0)
			{
				s.Name = g.Name + "_Header";
			}
            s.Visible = _chkHeader.Checked;
            if (s.Visible && s.Height == 0) s.Height = 1440;

			// update group footer section (no spaces in default name)
			s = g.SectionFooter;
			if (s.Name.Length == 0)
			{
				s.Name = g.Name + "_Footer";
			}
			s.Visible = _chkFooter.Checked;
            if (s.Visible && s.Height == 0) s.Height = 1440;
        }

		// move group up/down in list
		private void _btnUp_Click(object sender, System.EventArgs e)
		{
			Group g = ((GroupItem)_lstGroups.SelectedItem)._group;
			int index = _lstGroups.SelectedIndex - 1;
			g.Move(index, true);
			PopulateList(index);
		}
		private void _btnDown_Click(object sender, System.EventArgs e)
		{
			Group g = ((GroupItem)_lstGroups.SelectedItem)._group;
			int index = _lstGroups.SelectedIndex + 1;
			g.Move(index, true);
			PopulateList(index);
		}

		// add/delete groups
		private void _btnAdd_Click(object sender, System.EventArgs e)
		{
			// add new group
			Group g = _edit.Groups.Add(Strings.GroupEditorForm.NewGroupName, string.Empty, SortEnum.NoSort);

			// clear header/footer names, will be set later
			g.SectionHeader.Name = string.Empty;
			g.SectionFooter.Name = string.Empty;

			// refresh the display
			int cnt = _lstGroups.Items.Count;
			PopulateList(cnt);
		}
		private void _btnDelete_Click(object sender, System.EventArgs e)
		{
			// get selected group
			int index = _lstGroups.SelectedIndex;

			// remove it from the list
			if (_lstGroups.SelectedIndex >= 0)
				_edit.Groups.Remove(_lstGroups.SelectedIndex);

			// refresh the display
			PopulateList(index);
		}

		// when the group name changes, reset the list
		private void _txtName_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (_txtName.Text.Length == 0)
				_txtName.Text = _cmbGroupBy.Text;

			int index = _lstGroups.SelectedIndex;
			PopulateList(index);
		}

		// when GroupBy changes, update name if it hasn't changed yet
		private void _cmbGroupBy_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (_txtName.Text == Strings.GroupEditorForm.NewGroupName || _txtName.Text.Length == 0)
			{
				_txtName.Text = _cmbGroupBy.Text;
				int index = _lstGroups.SelectedIndex;
				PopulateList(index);
			}
		}
	}

	/// <summary>
	/// GroupItem
	/// helper class used to populate listbox
	/// </summary>
	internal class GroupItem
	{
		internal Group _group;

		internal GroupItem(Group group)
		{
			_group = group;
		}

        public override string ToString()
		{
			return _group.Name;
		}
	}
}
