#pragma once

#include <windows.h>
#include <tchar.h>
//#include <wstring>
//#include <string>
//#include <wchar.h>
//#include <WinUser.h>
#include <stdio.h>

#define GET_LPCTSTR_FROM_BOOLEAN(x) ((x) ? _T("1") : _T("0") )
#define GET_LPCTSTR_FROM_CHECKBOX(x) (GET_LPCTSTR_FROM_BOOLEAN((x)->Checked))

namespace MadogenSetting {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// オプション
	static TCHAR FILE_PATH[] = _T("Media/Option/Option.ini");

	/// ジェネラル
	static TCHAR GENERAL_FILE_PATH[] = _T("data/General.ini");

	/// 文字列変換バッファサイズ
	const int BUFFER_MAX = 1024;

	/// <summary>
	/// Form1 の概要
	///
	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
	///          変更する必要があります。この変更を行わないと、
	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
	///          正しく相互に利用できなくなります。
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクタ コードを追加します
			//
		}

	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::GroupBox^  groupBox1;
	private: System::Windows::Forms::CheckBox^  optKubi;
	protected: 

	private: System::Windows::Forms::Panel^  panel1;
	private: System::Windows::Forms::CheckBox^  optTransparent;
	private: System::Windows::Forms::Button^  btnCancel;
	private: System::Windows::Forms::Button^  btnOK;
	private: System::Windows::Forms::NumericUpDown^  numericUpDown3;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::NumericUpDown^  numericUpDown2;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::NumericUpDown^  numericUpDown1;
	private: System::Windows::Forms::CheckBox^  optMugen;
	private: System::Windows::Forms::CheckBox^  optNoCloth;
	private: System::Windows::Forms::CheckBox^  optBlack;
	private: System::Windows::Forms::GroupBox^  groupBox2;
	private: System::Windows::Forms::RadioButton^  gen1024x960;

	private: System::Windows::Forms::RadioButton^  gen800x600;

	private: System::Windows::Forms::RadioButton^  gen640x480;

	private: System::Windows::Forms::CheckBox^  genFullScreen;
	private: System::Windows::Forms::RadioButton^  gen1280x960;

	private:
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
			this->gen1024x960 = (gcnew System::Windows::Forms::RadioButton());
			this->gen800x600 = (gcnew System::Windows::Forms::RadioButton());
			this->gen640x480 = (gcnew System::Windows::Forms::RadioButton());
			this->genFullScreen = (gcnew System::Windows::Forms::CheckBox());
			this->numericUpDown3 = (gcnew System::Windows::Forms::NumericUpDown());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->numericUpDown2 = (gcnew System::Windows::Forms::NumericUpDown());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->numericUpDown1 = (gcnew System::Windows::Forms::NumericUpDown());
			this->optMugen = (gcnew System::Windows::Forms::CheckBox());
			this->optNoCloth = (gcnew System::Windows::Forms::CheckBox());
			this->optBlack = (gcnew System::Windows::Forms::CheckBox());
			this->optTransparent = (gcnew System::Windows::Forms::CheckBox());
			this->optKubi = (gcnew System::Windows::Forms::CheckBox());
			this->panel1 = (gcnew System::Windows::Forms::Panel());
			this->btnCancel = (gcnew System::Windows::Forms::Button());
			this->btnOK = (gcnew System::Windows::Forms::Button());
			this->gen1280x960 = (gcnew System::Windows::Forms::RadioButton());
			this->groupBox1->SuspendLayout();
			this->groupBox2->SuspendLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->numericUpDown3))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->numericUpDown2))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->numericUpDown1))->BeginInit();
			this->panel1->SuspendLayout();
			this->SuspendLayout();
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->groupBox2);
			this->groupBox1->Controls->Add(this->genFullScreen);
			this->groupBox1->Controls->Add(this->numericUpDown3);
			this->groupBox1->Controls->Add(this->label3);
			this->groupBox1->Controls->Add(this->numericUpDown2);
			this->groupBox1->Controls->Add(this->label2);
			this->groupBox1->Controls->Add(this->label1);
			this->groupBox1->Controls->Add(this->numericUpDown1);
			this->groupBox1->Controls->Add(this->optMugen);
			this->groupBox1->Controls->Add(this->optNoCloth);
			this->groupBox1->Controls->Add(this->optBlack);
			this->groupBox1->Controls->Add(this->optTransparent);
			this->groupBox1->Controls->Add(this->optKubi);
			this->groupBox1->Controls->Add(this->panel1);
			this->groupBox1->Dock = System::Windows::Forms::DockStyle::Fill;
			this->groupBox1->Location = System::Drawing::Point(0, 0);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(538, 355);
			this->groupBox1->TabIndex = 0;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = L"Evigen";
			// 
			// groupBox2
			// 
			this->groupBox2->Controls->Add(this->gen1280x960);
			this->groupBox2->Controls->Add(this->gen1024x960);
			this->groupBox2->Controls->Add(this->gen800x600);
			this->groupBox2->Controls->Add(this->gen640x480);
			this->groupBox2->Location = System::Drawing::Point(141, 42);
			this->groupBox2->Name = L"groupBox2";
			this->groupBox2->Size = System::Drawing::Size(105, 111);
			this->groupBox2->TabIndex = 14;
			this->groupBox2->TabStop = false;
			this->groupBox2->Text = L"Screen Size";
			// 
			// gen1024x960
			// 
			this->gen1024x960->AutoSize = true;
			this->gen1024x960->Location = System::Drawing::Point(7, 65);
			this->gen1024x960->Name = L"gen1024x960";
			this->gen1024x960->Size = System::Drawing::Size(71, 16);
			this->gen1024x960->TabIndex = 2;
			this->gen1024x960->TabStop = true;
			this->gen1024x960->Text = L"1024x768";
			this->gen1024x960->UseVisualStyleBackColor = true;
			// 
			// gen800x600
			// 
			this->gen800x600->AutoSize = true;
			this->gen800x600->Location = System::Drawing::Point(7, 42);
			this->gen800x600->Name = L"gen800x600";
			this->gen800x600->Size = System::Drawing::Size(65, 16);
			this->gen800x600->TabIndex = 1;
			this->gen800x600->TabStop = true;
			this->gen800x600->Text = L"800x600";
			this->gen800x600->UseVisualStyleBackColor = true;
			// 
			// gen640x480
			// 
			this->gen640x480->AutoSize = true;
			this->gen640x480->Location = System::Drawing::Point(7, 19);
			this->gen640x480->Name = L"gen640x480";
			this->gen640x480->Size = System::Drawing::Size(65, 16);
			this->gen640x480->TabIndex = 0;
			this->gen640x480->TabStop = true;
			this->gen640x480->Text = L"640x480";
			this->gen640x480->UseVisualStyleBackColor = true;
			// 
			// genFullScreen
			// 
			this->genFullScreen->AutoSize = true;
			this->genFullScreen->Location = System::Drawing::Point(140, 19);
			this->genFullScreen->Name = L"genFullScreen";
			this->genFullScreen->Size = System::Drawing::Size(82, 16);
			this->genFullScreen->TabIndex = 13;
			this->genFullScreen->Text = L"Full Screen";
			this->genFullScreen->UseVisualStyleBackColor = true;
			// 
			// numericUpDown3
			// 
			this->numericUpDown3->Location = System::Drawing::Point(7, 277);
			this->numericUpDown3->Name = L"numericUpDown3";
			this->numericUpDown3->Size = System::Drawing::Size(120, 19);
			this->numericUpDown3->TabIndex = 12;
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(6, 262);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(54, 12);
			this->label3->TabIndex = 11;
			this->label3->Text = L"BGM音量";
			// 
			// numericUpDown2
			// 
			this->numericUpDown2->Location = System::Drawing::Point(7, 230);
			this->numericUpDown2->Name = L"numericUpDown2";
			this->numericUpDown2->Size = System::Drawing::Size(120, 19);
			this->numericUpDown2->TabIndex = 10;
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(6, 215);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(79, 12);
			this->label2->TabIndex = 9;
			this->label2->Text = L"効果音音量(%)";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(5, 168);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(71, 12);
			this->label1->TabIndex = 8;
			this->label1->Text = L"ボイス音量(%)";
			// 
			// numericUpDown1
			// 
			this->numericUpDown1->Location = System::Drawing::Point(7, 183);
			this->numericUpDown1->Name = L"numericUpDown1";
			this->numericUpDown1->Size = System::Drawing::Size(120, 19);
			this->numericUpDown1->TabIndex = 7;
			// 
			// optMugen
			// 
			this->optMugen->AutoSize = true;
			this->optMugen->Location = System::Drawing::Point(7, 111);
			this->optMugen->Name = L"optMugen";
			this->optMugen->Size = System::Drawing::Size(86, 16);
			this->optMugen->TabIndex = 6;
			this->optMugen->Text = L"無限バンダナ";
			this->optMugen->UseVisualStyleBackColor = true;
			// 
			// optNoCloth
			// 
			this->optNoCloth->AutoSize = true;
			this->optNoCloth->Location = System::Drawing::Point(7, 88);
			this->optNoCloth->Name = L"optNoCloth";
			this->optNoCloth->Size = System::Drawing::Size(59, 16);
			this->optNoCloth->TabIndex = 5;
			this->optNoCloth->Text = L"忘れ物";
			this->optNoCloth->UseVisualStyleBackColor = true;
			// 
			// optBlack
			// 
			this->optBlack->AutoSize = true;
			this->optBlack->Location = System::Drawing::Point(7, 65);
			this->optBlack->Name = L"optBlack";
			this->optBlack->Size = System::Drawing::Size(90, 16);
			this->optBlack->TabIndex = 4;
			this->optBlack->Text = L"大人なブラック";
			this->optBlack->UseVisualStyleBackColor = true;
			// 
			// optTransparent
			// 
			this->optTransparent->AutoSize = true;
			this->optTransparent->Location = System::Drawing::Point(7, 42);
			this->optTransparent->Name = L"optTransparent";
			this->optTransparent->Size = System::Drawing::Size(127, 16);
			this->optTransparent->TabIndex = 3;
			this->optTransparent->Text = L"飲み込まれ時透明化";
			this->optTransparent->UseVisualStyleBackColor = true;
			// 
			// optKubi
			// 
			this->optKubi->AutoSize = true;
			this->optKubi->Location = System::Drawing::Point(7, 19);
			this->optKubi->Name = L"optKubi";
			this->optKubi->Size = System::Drawing::Size(72, 16);
			this->optKubi->TabIndex = 2;
			this->optKubi->Text = L"切断表現";
			this->optKubi->UseVisualStyleBackColor = true;
			// 
			// panel1
			// 
			this->panel1->Controls->Add(this->btnCancel);
			this->panel1->Controls->Add(this->btnOK);
			this->panel1->Dock = System::Windows::Forms::DockStyle::Bottom;
			this->panel1->Location = System::Drawing::Point(3, 323);
			this->panel1->Name = L"panel1";
			this->panel1->Size = System::Drawing::Size(532, 29);
			this->panel1->TabIndex = 1;
			// 
			// btnCancel
			// 
			this->btnCancel->DialogResult = System::Windows::Forms::DialogResult::Cancel;
			this->btnCancel->Location = System::Drawing::Point(448, 3);
			this->btnCancel->Name = L"btnCancel";
			this->btnCancel->Size = System::Drawing::Size(75, 23);
			this->btnCancel->TabIndex = 1;
			this->btnCancel->Text = L"Cancel";
			this->btnCancel->UseVisualStyleBackColor = true;
			this->btnCancel->Click += gcnew System::EventHandler(this, &Form1::btnCancel_Click);
			// 
			// btnOK
			// 
			this->btnOK->DialogResult = System::Windows::Forms::DialogResult::OK;
			this->btnOK->Location = System::Drawing::Point(324, 3);
			this->btnOK->Name = L"btnOK";
			this->btnOK->Size = System::Drawing::Size(118, 23);
			this->btnOK->TabIndex = 0;
			this->btnOK->Text = L"Save and Close";
			this->btnOK->UseVisualStyleBackColor = true;
			this->btnOK->Click += gcnew System::EventHandler(this, &Form1::btnOK_Click);
			// 
			// gen1280x960
			// 
			this->gen1280x960->AutoSize = true;
			this->gen1280x960->Location = System::Drawing::Point(7, 88);
			this->gen1280x960->Name = L"gen1280x960";
			this->gen1280x960->Size = System::Drawing::Size(71, 16);
			this->gen1280x960->TabIndex = 3;
			this->gen1280x960->TabStop = true;
			this->gen1280x960->Text = L"1280x960";
			this->gen1280x960->UseVisualStyleBackColor = true;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(538, 355);
			this->Controls->Add(this->groupBox1);
			this->Name = L"Form1";
			this->Text = L"Madogen Setting Editor";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->groupBox1->ResumeLayout(false);
			this->groupBox1->PerformLayout();
			this->groupBox2->ResumeLayout(false);
			this->groupBox2->PerformLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->numericUpDown3))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->numericUpDown2))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->numericUpDown1))->EndInit();
			this->panel1->ResumeLayout(false);
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
				 this->optKubi->Checked = GetPrivateProfileInt( _T("Evigen"), _T("Kubi"),
					 1, FILE_PATH ) == 1;
				 this->optTransparent->Checked = GetPrivateProfileInt( _T("Evigen"), _T("Transparent"),
					 0, FILE_PATH ) == 1;
				 this->optBlack->Checked = GetPrivateProfileInt( _T("Evigen"), _T("Black"),
					 1, FILE_PATH ) == 1;
				 this->optNoCloth->Checked = GetPrivateProfileInt( _T("Evigen"), _T("NoCloth"),
					 0, FILE_PATH ) == 1;
				 this->optMugen->Checked = GetPrivateProfileInt( _T("Evigen"), _T("Mugen"),
					 1, FILE_PATH ) == 1;
				 
				 int num = GetPrivateProfileInt( _T("Evigen"), _T("Voice"),
					 1, FILE_PATH );
				 this->numericUpDown1->Text = num.ToString();
				 this->numericUpDown2->Text = 
					 GetPrivateProfileInt( _T("Evigen"), _T("SE"),
					 1, FILE_PATH ).ToString();
				 this->numericUpDown3->Text = 
					 GetPrivateProfileInt( _T("Evigen"), _T("BGM"),
					 1, FILE_PATH ).ToString();

				 // General
				 this->genFullScreen->Checked = GetPrivateProfileInt( _T("Graphics"), _T("FullScreenFlag"),
					 0, GENERAL_FILE_PATH) == 1;
				 this->gen640x480->Checked = GetPrivateProfileInt( _T("Graphics"), _T("WindowWidth"),
					 640, GENERAL_FILE_PATH) == 640;
				 this->gen800x600->Checked = GetPrivateProfileInt( _T("Graphics"), _T("WindowWidth"),
					 800, GENERAL_FILE_PATH) == 800;
				 this->gen1024x960->Checked = GetPrivateProfileInt( _T("Graphics"), _T("WindowWidth"),
					 1024, GENERAL_FILE_PATH) == 1024;
				 this->gen1280x960->Checked = GetPrivateProfileInt( _T("Graphics"), _T("WindowWidth"),
					 1280, GENERAL_FILE_PATH) == 1280;
			 }

private: System::Void convertIntToTCHAR( TCHAR* lpszBuffer, int num ) {
#ifdef UNICODE
				 wsprintf( lpszBuffer, _T("%d"), num);
#else
				 sprintf_s( lpszBuffer, BUFFER_MAX, "%d", num );
#endif
		 }
private: System::Void btnOK_Click(System::Object^  sender, System::EventArgs^  e) {
			 WritePrivateProfileString( _T("Evigen"), _T("Kubi"),
				 GET_LPCTSTR_FROM_CHECKBOX(this->optKubi ), FILE_PATH );

			 
			 WritePrivateProfileString( _T("Evigen"), _T("Transparent"),
				 GET_LPCTSTR_FROM_CHECKBOX(this->optTransparent), FILE_PATH );

			 WritePrivateProfileString( _T("Evigen"), _T("Black"),
				GET_LPCTSTR_FROM_CHECKBOX(this->optBlack), FILE_PATH );

			 WritePrivateProfileString( _T("Evigen"), _T("NoCloth"),
				 GET_LPCTSTR_FROM_CHECKBOX(this->optNoCloth), FILE_PATH );

			 WritePrivateProfileString( _T("Evigen"), _T("Mugen"),
				 GET_LPCTSTR_FROM_CHECKBOX(this->optMugen), FILE_PATH );

			 // general
			 //
			 WritePrivateProfileString( _T("Graphics"), _T("FullScreenFlag"),
				 GET_LPCTSTR_FROM_CHECKBOX(this->genFullScreen), GENERAL_FILE_PATH);
			 if( this->gen640x480->Checked ) {
				 WritePrivateProfileString( _T("Graphics"), _T("WindowWidth"),
					 _T("640"), GENERAL_FILE_PATH);
				 WritePrivateProfileString( _T("Graphics"), _T("WindowHeight"),
					 _T("480"), GENERAL_FILE_PATH);
			 } else if( this->gen800x600->Checked ) {
				 WritePrivateProfileString( _T("Graphics"), _T("WindowWidth"),
					 _T("800"), GENERAL_FILE_PATH);
				 WritePrivateProfileString( _T("Graphics"), _T("WindowHeight"),
					 _T("600"), GENERAL_FILE_PATH);
			 } else if( this->gen1024x960->Checked ) {
				 WritePrivateProfileString( _T("Graphics"), _T("WindowWidth"),
					 _T("1024"), GENERAL_FILE_PATH);
				 WritePrivateProfileString( _T("Graphics"), _T("WindowHeight"),
					 _T("768"), GENERAL_FILE_PATH);
			 } else if( this->gen1280x960->Checked ) {
				 WritePrivateProfileString( _T("Graphics"), _T("WindowWidth"),
					 _T("1280"), GENERAL_FILE_PATH);
				 WritePrivateProfileString( _T("Graphics"), _T("WindowHeight"),
					 _T("960"), GENERAL_FILE_PATH);
			 }
			 TCHAR lpszBuffer[BUFFER_MAX];

			 try {
				 this->convertIntToTCHAR( lpszBuffer, Convert::ToInt32( this->numericUpDown1->Text  ) );
				 WritePrivateProfileString( _T("Evigen"), _T("Voice"),
					 lpszBuffer, FILE_PATH );

				 this->convertIntToTCHAR( lpszBuffer, Convert::ToInt32( this->numericUpDown2->Text  ) );
				 WritePrivateProfileString( _T("Evigen"), _T("SE"),
					 lpszBuffer, FILE_PATH );

				 this->convertIntToTCHAR( lpszBuffer, Convert::ToInt32( this->numericUpDown3->Text  ) );
				 WritePrivateProfileString( _T("Evigen"), _T("BGM"),
					lpszBuffer, FILE_PATH );

			 }catch(System::Exception^ e ) {
				 // do nothing
				 e;
			 } finally {

				 Application::Exit();
			 }
		 }
private: System::Void btnCancel_Click(System::Object^  sender, System::EventArgs^  e) {
			 Application::Exit();

		 }
};
}

