//----------------------------------------------------------------------------
// LinkSubreportForm.cs
//
// Subreport link dialog for use with C1ReportDesigner app
// Allows the user to specify a connection between a main report
// and a subreport via a common field on their data sources.
//
// 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.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using C1.C1Report;
using C1.Win.C1ReportDesigner;

namespace ReportDesignerApp.Util
{
	/// <summary>
	/// Summary description for LinkSubreportForm.
	/// </summary>
	public class LinkSubreportForm : System.Windows.Forms.Form
	{
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button _btnOK;
        private System.Windows.Forms.Button _btnCancel;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.ComboBox _cmbMaster;
        private System.Windows.Forms.ComboBox _cmbChild;
        private System.Windows.Forms.Label label4;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public LinkSubreportForm(C1ReportDesigner designer)
		{
			// Required for Windows Form Designer support
			InitializeComponent();

			// TODO: Add any constructor code after InitializeComponent call
            AutoScaleMode = AutoScaleMode.None;
            Font = MainForm.DefaultAppFont;
            _designer = designer;
        }

		/// <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()
		{
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this._btnOK = new System.Windows.Forms.Button();
            this._btnCancel = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this._cmbMaster = new System.Windows.Forms.ComboBox();
            this._cmbChild = new System.Windows.Forms.ComboBox();
            this.label4 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(7, 104);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(147, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Container report field (master):";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(7, 140);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(165, 13);
            this.label2.TabIndex = 0;
            this.label2.Text = "Must match subreport field (child):";
            // 
            // _btnOK
            // 
            this._btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this._btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
            this._btnOK.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this._btnOK.Location = new System.Drawing.Point(266, 184);
            this._btnOK.Name = "_btnOK";
            this._btnOK.Size = new System.Drawing.Size(88, 24);
            this._btnOK.TabIndex = 1;
            this._btnOK.Text = "&OK";
            this._btnOK.Click += new System.EventHandler(this._btnOK_Click);
            // 
            // _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.Location = new System.Drawing.Point(362, 184);
            this._btnCancel.Name = "_btnCancel";
            this._btnCancel.Size = new System.Drawing.Size(88, 24);
            this._btnCancel.TabIndex = 1;
            this._btnCancel.Text = "&Cancel";
            // 
            // label3
            // 
            this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.label3.Location = new System.Drawing.Point(7, 8);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(450, 37);
            this.label3.TabIndex = 2;
            this.label3.Text = "Define which records will be included in the subreport by specifying a master fie" +
                "ld in the main report and a child field in the subreport.";
            // 
            // _cmbMaster
            // 
            this._cmbMaster.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this._cmbMaster.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this._cmbMaster.Location = new System.Drawing.Point(208, 100);
            this._cmbMaster.Name = "_cmbMaster";
            this._cmbMaster.Size = new System.Drawing.Size(242, 21);
            this._cmbMaster.TabIndex = 3;
            this._cmbMaster.SelectedIndexChanged += new System.EventHandler(this._cmb_SelectedIndexChanged);
            // 
            // _cmbChild
            // 
            this._cmbChild.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this._cmbChild.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this._cmbChild.Location = new System.Drawing.Point(208, 136);
            this._cmbChild.Name = "_cmbChild";
            this._cmbChild.Size = new System.Drawing.Size(242, 21);
            this._cmbChild.TabIndex = 3;
            this._cmbChild.SelectedIndexChanged += new System.EventHandler(this._cmb_SelectedIndexChanged);
            // 
            // label4
            // 
            this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.label4.Location = new System.Drawing.Point(7, 48);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(450, 38);
            this.label4.TabIndex = 2;
            this.label4.Text = "(This will set the Text property on the subreport field to an expression that wil" +
                "l be used as a filter on the subreport data source.)";
            // 
            // LinkSubreportForm
            // 
            this.AcceptButton = this._btnOK;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this._btnCancel;
            this.ClientSize = new System.Drawing.Size(466, 215);
            this.Controls.Add(this._cmbMaster);
            this.Controls.Add(this.label3);
            this.Controls.Add(this._btnOK);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this._btnCancel);
            this.Controls.Add(this._cmbChild);
            this.Controls.Add(this.label4);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "LinkSubreportForm";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Link Subreport";
            this.Load += new System.EventHandler(this.LinkSubreportForm_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

        private C1ReportDesigner _designer;
        private Field            _subrepField;

        private void LinkSubreportForm_Load(object sender, System.EventArgs e)
        {
            // code moved to Init() to avoid flashing the dialog if it cannot proceed. TFS~~11451.
        }

        internal bool Init()
        {
            // get selected field (should be a subreport)
            _subrepField = null;
            foreach (Field sf in _designer.SelectedFields)
            {
                if (sf.Subreport == null) continue;
                _subrepField = sf;
                break;
            }
            if (_subrepField == null)
            {
                MessageBox.Show("Can't find selected subreport field.");
                return false;
            }

            // populate master and child lists with container report data fields
            try
            {
                foreach (string s in _designer.Report.DataSource.GetDBFieldList(true))
                    _cmbMaster.Items.Add(s);
            }
            catch
            {
                MessageBox.Show("Could not retrieve fields for master report.");
                return false;
            }
            try
            {
                foreach (string s in _subrepField.Subreport.DataSource.GetDBFieldList(true))
                    _cmbChild.Items.Add(s);
            }
            catch
            {
                MessageBox.Show("Could not retrieve fields for subreport.");
                return false;
            }

			// try to find a match automatically
			foreach (string sm in _cmbMaster.Items)
			{
				foreach (string sc in _cmbChild.Items)
				{
					if (sm == sc)
					{
						_cmbMaster.Text = sm;
						_cmbChild.Text = sc;
						break;
					}
				}
			}

            // all's ok
            return true;
        }

        // enable OK button after both fields are selected
        private void _cmb_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            _btnOK.Enabled = (_cmbMaster.SelectedIndex > -1 && _cmbChild.SelectedIndex > -1);
        }

        // done, build expression and return
        private void _btnOK_Click(object sender, System.EventArgs e)
        {
			if (_cmbMaster.Text.Length > 0 && _cmbChild.Text.Length > 0) // <<B167>>
			{
				string master = Bracket(_cmbMaster.Text);
				string child  = Bracket(_cmbChild.Text);
				string expr = string.Format("\"{0} = '\" & {1} & \"'\"", child, master);
				_subrepField.Text = expr;        
				_subrepField.Calculated = true;
			}
        }

        // bracket field names if necessary
        private string Bracket(string name)
        {
            return (name.StartsWith("[") && name.EndsWith("]"))
                ? name
                : string.Format("[{0}]", name);
        }
    }
}
