using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace NewsCrawler
{
	public partial class ConfigForm : Form
	{
		NewsForm m_Listener = null;

		public ConfigForm(NewsForm Listener)
		{
			m_Listener = Listener;
			InitializeComponent();
		}

		private void ConfigForm_Load(object sender, EventArgs e)
		{
			tbVolume.Text = Config.GetManualPrice().ToString();
			tbBuyPrice.Text = Config.GetBuyPrice().ToString();

			cbAccount.Items.Clear();
			string[] Accounts = m_Listener.GetAccounts();
			cbAccount.Items.AddRange(Accounts);
			if(Config.GetAccount() != "" && Accounts.Contains(Config.GetAccount()))
				cbAccount.SelectedItem = Config.GetAccount();

			tbAnSupplyContractRate.Text = Config.GetSupplyContractRate().ToString();
			tbAnRevenueRate.Text = Config.GetRevenueRate().ToString();
		}

		private void OpenFile(string strName)
		{
			string strPath = Util.GetConfigPath()+strName;
			if(File.Exists(strPath) == false)
			{
				using(var sw = new StreamWriter(File.Open(strPath, FileMode.CreateNew), new UTF8Encoding(true)))
					sw.WriteLine("");
			}

			System.Diagnostics.Process.Start(strPath);
		}

		#region codes
		private void btnCodeManualMake_Click(object sender, EventArgs e)
		{
			int iPrice;
			int.TryParse(tbVolume.Text, out iPrice);

			Config.SetManualPrice(iPrice);
			m_Listener.OnManualCodeClick(iPrice);
		}

		private void btnCodeManualEdit_Click(object sender, EventArgs e)
		{
			OpenFile("/code-manual.txt");
		}

		private void btnDenialCodeEdit_Click(object sender, EventArgs e)
		{
			OpenFile("/code-deny.txt");
		}

		private void btnDenialCodeApply_Click(object sender, EventArgs e)
		{
			m_Listener.ApplyDenialCode();
		}

		private void btnDuplicatedCodeEdit_Click(object sender, EventArgs e)
		{
			OpenFile("/code-duplicated.txt");
		}

		private void btnDuplicatedCodeApply_Click(object sender, EventArgs e)
		{
			m_Listener.ApplyDuplicatedCode();
		}
		#endregion

		#region Keywords
		private void btnPositiveEdit_Click(object sender, EventArgs e)
		{
			OpenFile("/keyword-positive.txt");
		}

		private void btnPositiveApply_Click(object sender, EventArgs e)
		{
			m_Listener.ApplyPositive();
		}

		private void btnManualEdit_Click(object sender, EventArgs e)
		{
			OpenFile("/keyword-manual.txt");
		}

		private void btnManualApply_Click(object sender, EventArgs e)
		{
			m_Listener.ApplyManual();
		}

		private void btnNegativeEdit_Click(object sender, EventArgs e)
		{
			OpenFile("/keyword-negative.txt");
		}

		private void btnNegativeApply_Click(object sender, EventArgs e)
		{
			m_Listener.ApplyNegative();
		}

		private void btnSynonymEdit_Click(object sender, EventArgs e)
		{
			OpenFile("/code-synonym.txt");
		}

		private void btnSynonymApply_Click(object sender, EventArgs e)
		{
			m_Listener.ApplySynonym();
		}

		private void btnDuplicatedKeywordEdit_Click(object sender, EventArgs e)
		{
			OpenFile("/keyword-duplicated.txt");
		}

		private void btnDuplicatedKeywordApply_Click(object sender, EventArgs e)
		{
			m_Listener.ApplyDuplicatedKeyword();
		}
		#endregion

		#region Buy
		private void tbBuyPrice_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !char.IsDigit(e.KeyChar);
		}

		private void tbBuyPrice_TextChanged(object sender, EventArgs e)
		{
			int iPrice;
			bool bNumber = int.TryParse(tbBuyPrice.Text, out iPrice);
			Config.SetBuyPrice(iPrice);
		}
		#endregion

		private void btnAccountApply_Click(object sender, EventArgs e)
		{
			m_Listener.ApplyAccount(cbAccount.SelectedItem as string, tbAccountSub.Text);
		}

		private void ConfigForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			m_Listener.OnConfigFormClosing();
		}

		private void tbSupplyContractRate_KeyPress(object sender, KeyPressEventArgs e)
		{
			if(char.IsDigit(e.KeyChar) == false && e.KeyChar != '.')
				e.Handled = true;
		}

		private void tbRevenueRate_KeyPress(object sender, KeyPressEventArgs e)
		{
			if(char.IsDigit(e.KeyChar) == false && e.KeyChar != '.')
				e.Handled = true;
		}

		private void tbAnProfitChange_KeyPress(object sender, KeyPressEventArgs e)
		{
			if(char.IsDigit(e.KeyChar) == false && e.KeyChar != '.')
				e.Handled = true;
		}

		private void btAnApply_Click(object sender, EventArgs e)
		{
			float fSupplyContractRate;
			float.TryParse(tbAnSupplyContractRate.Text, out fSupplyContractRate);

			float fRevenueRate;
			float.TryParse(tbAnRevenueRate.Text, out fRevenueRate);

			float fProfitChange;
			float.TryParse(tbAnProfitChange.Text, out fProfitChange);

			Config.SetAnnouncement(chAnDartAPI.Checked,
				chAnSupply.Checked, fSupplyContractRate,
				chAnRevenue.Checked, fRevenueRate,
				chAnRightsIssue.Checked,
				chAnPatent.Checked, tbAnPatent.Text,
				chAnProfitChange.Checked, fProfitChange);
		}

		private void cbMockTrading_CheckedChanged(object sender, EventArgs e)
		{
			Config.SetMockTrading(cbMockTrading.Checked);
		}
	}
}
