using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using Zanetti.SystemTrading.AutoTrading;
using Zanetti.UI;

namespace Zanetti.Forms
{
	/// <summary>
	/// AutoTradingSummaryDialog の概要の説明です。
	/// </summary>
	internal class AutoTradingSummaryDialog : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button _copyButton;
		private System.Windows.Forms.Button _okButton;
		private System.Windows.Forms.ListView _listView;
		private System.Windows.Forms.ColumnHeader _nameColumn;
		private System.Windows.Forms.ColumnHeader _valueColumn;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public AutoTradingSummaryDialog(AutoTradingResult sr)
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
			//
			InitUI(sr);
		}

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows フォーム デザイナで生成されたコード 
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			this._copyButton = new System.Windows.Forms.Button();
			this._okButton = new System.Windows.Forms.Button();
			this._listView = new System.Windows.Forms.ListView();
			this._nameColumn = new System.Windows.Forms.ColumnHeader();
			this._valueColumn = new System.Windows.Forms.ColumnHeader();
			this.SuspendLayout();
			// 
			// _copyButton
			// 
			this._copyButton.Location = new System.Drawing.Point(24, 352);
			this._copyButton.Name = "_copyButton";
			this._copyButton.TabIndex = 2;
			this._copyButton.Text = "コピー";
			this._copyButton.Click += new System.EventHandler(this.OnCopyButton);
			this._copyButton.FlatStyle = FlatStyle.System;
			// 
			// _okButton
			// 
			this._okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
			this._okButton.Location = new System.Drawing.Point(288, 352);
			this._okButton.Name = "_okButton";
			this._okButton.TabIndex = 0;
			this._okButton.Text = "OK";
			this._okButton.FlatStyle = FlatStyle.System;
			// 
			// _listView
			// 
			this._listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this._nameColumn,
																						this._valueColumn});
			this._listView.GridLines = true;
			this._listView.Location = new System.Drawing.Point(0, 0);
			this._listView.MultiSelect = false;
			this._listView.Name = "_listView";
			this._listView.Size = new System.Drawing.Size(376, 344);
			this._listView.TabIndex = 1;
			this._listView.View = System.Windows.Forms.View.Details;
			// 
			// _nameColumn
			// 
			this._nameColumn.Text = "項目";
			this._nameColumn.Width = 160;
			// 
			// _valueColumn
			// 
			this._valueColumn.Text = "値";
			this._valueColumn.Width = 208;
			// 
			// AutoTradingSummaryDialog
			// 
			this.AcceptButton = this._okButton;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.CancelButton = this._okButton;
			this.ClientSize = new System.Drawing.Size(376, 383);
			this.ControlBox = false;
			this.Controls.Add(this._listView);
			this.Controls.Add(this._okButton);
			this.Controls.Add(this._copyButton);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "AutoTradingSummaryDialog";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "自動売買の集計";
			this.ResumeLayout(false);

		}
		#endregion

		private void InitUI(AutoTradingResult sr) {
			AutoTradingResultSummary sum = sr.CreateSummary();
			AddItem("検証対象銘柄", sr.BrandDescription);
            //AddItem("シグナル発生時に使う価格", );
			AddItem("検証開始日", Util.FormatFullDate(sr.StartDate));
			AddItem("検証終了日", Util.FormatFullDate(sr.EndDate));
			AddItem("総検証日数", String.Format("{0}日", sr.TotalCheckCount));
			AddItem("シグナル発生数", String.Format("{0}回 (平均して{1:F2}日に１回)", sr.ResultCount, (double)sr.TotalCheckCount / sr.ResultCount));
			AddItem("１シグナルでの平均トレード日数", String.Format("{0:F2}日", sum._avgTradeLength));
			AddItem("１トレードの平均パフォーマンス", String.Format("{0:F2}%", sum._avgPerformance * 100));
			AddItem("パフォーマンスの標準偏差", String.Format("{0:F2}%", sum._sdTotal * 100));

			AddItem("成功数", String.Format("{0} ({1:F2}%)", sum._exitCount, (double)sum._exitCount / sum._totalCount * 100));
			AddItem("成功時の平均利益", String.Format("{0:F2}%", sum._avgExit * 100));
			AddItem("成功時の標準偏差", String.Format("{0:F2}%", sum._sdExit * 100));
			AddItem("成功時の平均トレード期間", String.Format("{0:F2}日", sum._avgExitTradeLength));
			SignalResult t = sum._maxExit;
			if(t!=null)
				AddItem("最大利益", String.Format("{0:F2}% ({1}{2} {3})", t.Performance(sr.Item.TradingType)*100, t.Brand.CodeAsString, t.Brand.Name, Util.FormatShortDate(t.StartDate)));
			else
				AddItem("最大利益", "-");

			AddItem("失敗数", String.Format("{0} ({1:F2}%)", sum._losscutCount, (double)sum._losscutCount / sum._totalCount * 100));
			AddItem("失敗時の平均損失", String.Format("{0:F2}%", sum._avgLosscut * 100));
			AddItem("失敗時の標準偏差", String.Format("{0:F2}%", sum._sdLosscut * 100));
			AddItem("失敗時の平均トレード期間", String.Format("{0:F2}日", sum._avgLosscutTradeLength));
			t = sum._maxLosscut;
			if(t!=null)
				AddItem("最大損失", String.Format("{0:F2}% ({1}{2} {3})", t.Performance(sr.Item.TradingType)*100, t.Brand.CodeAsString, t.Brand.Name, Util.FormatShortDate(t.StartDate)));
			else
				AddItem("最大損失", "-");

			AddItem("結果判定不能なシグナル数", (sr.ResultCount - sum._exitCount - sum._losscutCount).ToString());
		}

		private void AddItem(string name, string value) {
			ListViewItem li = new ListViewItem(new string[] { name, value });
			_listView.Items.Add(li);
		}

		// Copyボタン
		// ListViewUtilを流用し、一応動作しているものの自信はなし by Yakutainashi
		private void OnCopyButton(object sender, EventArgs e) {
			ListViewUtil.CopyListViewContentToClipboard(_listView, new ListViewUtil.ItemChecker(ListViewItemChecker));
		}
		private static bool ListViewItemChecker(ListViewItem li) {
			return true;
		}
	}
}
