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();
		}

		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();
		}
		#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();
		}
	}
}
