using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace KeySetting
{
	/// <summary>
	/// MainWindow.xaml の相互作用ロジック
	/// </summary>
	public partial class MainWindow : Window
	{

		System.Windows.Forms.TextBox winTextBox1 = new System.Windows.Forms.TextBox() { Text = "0", ReadOnly = true, ImeMode = System.Windows.Forms.ImeMode.Off };
		System.Windows.Forms.TextBox winTextBox2 = new System.Windows.Forms.TextBox() { Text = "0", ReadOnly = true, ImeMode = System.Windows.Forms.ImeMode.Off };
		System.Windows.Forms.TextBox winTextBox3 = new System.Windows.Forms.TextBox() { Text = "0", ReadOnly = true, ImeMode = System.Windows.Forms.ImeMode.Off };
		System.Windows.Forms.TextBox winTextBox4 = new System.Windows.Forms.TextBox() { Text = "0", ReadOnly = true, ImeMode = System.Windows.Forms.ImeMode.Off };
		System.Windows.Forms.TextBox winTextBox5 = new System.Windows.Forms.TextBox() { Text = "0", ReadOnly = true, ImeMode = System.Windows.Forms.ImeMode.Off };
		System.Windows.Forms.TextBox winTextBox6 = new System.Windows.Forms.TextBox() { Text = "0", ReadOnly = true, ImeMode = System.Windows.Forms.ImeMode.Off };
		
		public MainWindow()
		{
			InitializeComponent();

			winTextBox1.KeyUp += textBox1_KeyUp;
			winTextBox2.KeyUp += textBox1_KeyUp;
			winTextBox3.KeyUp += textBox1_KeyUp;
			winTextBox4.KeyUp += textBox1_KeyUp;
			winTextBox5.KeyUp += textBox1_KeyUp;
			winTextBox6.KeyUp += textBox1_KeyUp;
		}

		public string FileName
		{
			get
			{
				return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\key.setting";
			}
		}

		private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			((System.Windows.Forms.TextBox)sender).Text = ((int) e.KeyCode).ToString();
		}

		private void button2_Click(object sender, RoutedEventArgs e)
		{
			this.WindowState = System.Windows.WindowState.Minimized;
			this.textBlock1.Focus();
		}

		private void button1_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{

			this.textBox1.Child = winTextBox1;
			this.textBox2.Child = winTextBox2;
			this.textBox3.Child = winTextBox3;
			this.textBox4.Child = winTextBox4;
			this.textBox5.Child = winTextBox5;
			this.textBox6.Child = winTextBox6;
			
			if (System.IO.File.Exists(FileName))
			{
				var reader = new System.IO.StreamReader(FileName);
				try
				{
					this.winTextBox1.Text = int.Parse(reader.ReadLine()).ToString();
					this.winTextBox2.Text = int.Parse(reader.ReadLine()).ToString();
					this.winTextBox3.Text = int.Parse(reader.ReadLine()).ToString();
					this.winTextBox4.Text = int.Parse(reader.ReadLine()).ToString();
					this.winTextBox5.Text = int.Parse(reader.ReadLine()).ToString();
					this.winTextBox6.Text = int.Parse(reader.ReadLine()).ToString();
				}
				catch (Exception)
				{
					if (MessageBox.Show("ファイルの読み込みに失敗しました。設定を初期化しますか?", "失敗", MessageBoxButton.YesNo) == MessageBoxResult.No)
					{
						reader.Close();
						this.Close();
						return;
					}
				}
				finally
				{
					reader.Close();
				}
			}
		}

		private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			var reader = new System.IO.StreamWriter(FileName);
			try
			{
				reader.WriteLine(this.winTextBox1.Text);
				reader.WriteLine(this.winTextBox2.Text);
				reader.WriteLine(this.winTextBox3.Text);
				reader.WriteLine(this.winTextBox4.Text);
				reader.WriteLine(this.winTextBox5.Text);
				reader.WriteLine(this.winTextBox6.Text);
			}
			catch (Exception)
			{
				MessageBox.Show("ファイルの書き込みに失敗しました。");
			}
			finally
			{
				reader.Close();
			};
		}

	}
}
