#pragma once
#include "texcontext.h"

namespace DBLFucker {

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

	/// <summary>
	/// Summary for wtexcontexted
	/// </summary>
	public ref class wtexcontexted : public System::Windows::Forms::Form
	{
	public:
		char* szChunkfilename;
	private: System::Windows::Forms::PictureBox^  pictureBox1;
	public: 
		char* szTexchunkfname;
		wtexcontexted(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}
	private: System::Windows::Forms::Label^  label3;
	public: 
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::TextBox^  textBox3;
	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::TextBox^  textBox4;
	private: System::Windows::Forms::Label^  label6;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Button^  button2;
	private:
		FILE* pTexChunk;
		TexContextList_s* pTexContextList;
		tTextureList_s* pTexList;
		Bitmap^ pTextureBitmap;
	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~wtexcontexted()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Label^  label1;
	protected: 
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::ListBox^  listBox1;
	private: System::Windows::Forms::Label^  label2;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->listBox1 = (gcnew System::Windows::Forms::ListBox());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->textBox4 = (gcnew System::Windows::Forms::TextBox());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(12, 9);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(117, 13);
			this->label1->TabIndex = 0;
			this->label1->Text = L"Texture context chunk:";
			// 
			// textBox1
			// 
			this->textBox1->BackColor = System::Drawing::SystemColors::Control;
			this->textBox1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
			this->textBox1->Location = System::Drawing::Point(135, 6);
			this->textBox1->Name = L"textBox1";
			this->textBox1->ReadOnly = true;
			this->textBox1->Size = System::Drawing::Size(250, 20);
			this->textBox1->TabIndex = 1;
			// 
			// listBox1
			// 
			this->listBox1->FormattingEnabled = true;
			this->listBox1->Location = System::Drawing::Point(15, 43);
			this->listBox1->Name = L"listBox1";
			this->listBox1->Size = System::Drawing::Size(175, 212);
			this->listBox1->TabIndex = 2;
			this->listBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &wtexcontexted::listBox1_SelectedIndexChanged);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(12, 27);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(80, 13);
			this->label2->TabIndex = 3;
			this->label2->Text = L"Context entries:";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(196, 27);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(83, 13);
			this->label3->TabIndex = 4;
			this->label3->Text = L"Entry properties:";
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(196, 43);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(107, 13);
			this->label4->TabIndex = 5;
			this->label4->Text = L"Associated texture #:";
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(309, 40);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(76, 20);
			this->textBox2->TabIndex = 6;
			// 
			// textBox3
			// 
			this->textBox3->Location = System::Drawing::Point(309, 66);
			this->textBox3->Name = L"textBox3";
			this->textBox3->Size = System::Drawing::Size(76, 20);
			this->textBox3->TabIndex = 8;
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(196, 69);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(90, 13);
			this->label5->TabIndex = 7;
			this->label5->Text = L"Associated mesh:";
			// 
			// textBox4
			// 
			this->textBox4->Location = System::Drawing::Point(309, 92);
			this->textBox4->Name = L"textBox4";
			this->textBox4->Size = System::Drawing::Size(76, 20);
			this->textBox4->TabIndex = 10;
			// 
			// label6
			// 
			this->label6->AutoSize = true;
			this->label6->Location = System::Drawing::Point(196, 95);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(43, 13);
			this->label6->TabIndex = 9;
			this->label6->Text = L"Effects:";
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(310, 232);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 11;
			this->button1->Text = L"Save";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &wtexcontexted::button1_Click);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(199, 118);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(185, 23);
			this->button2->TabIndex = 12;
			this->button2->Text = L"Apply new properties";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &wtexcontexted::button2_Click);
			// 
			// pictureBox1
			// 
			this->pictureBox1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
			this->pictureBox1->Location = System::Drawing::Point(235, 147);
			this->pictureBox1->Name = L"pictureBox1";
			this->pictureBox1->Size = System::Drawing::Size(114, 79);
			this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
			this->pictureBox1->TabIndex = 13;
			this->pictureBox1->TabStop = false;
			// 
			// wtexcontexted
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(397, 261);
			this->Controls->Add(this->pictureBox1);
			this->Controls->Add(this->listBox1);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->textBox4);
			this->Controls->Add(this->label6);
			this->Controls->Add(this->textBox3);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->label1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedToolWindow;
			this->Name = L"wtexcontexted";
			this->ShowIcon = false;
			this->Text = L"Texture Context Editor";
			this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &wtexcontexted::wtexcontexted_FormClosing);
			this->Load += gcnew System::EventHandler(this, &wtexcontexted::wtexcontexted_Load);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void wtexcontexted_Load(System::Object^  sender, System::EventArgs^  e) {
				 textBox1->Text = MakeString( szChunkfilename );
				 pTexContextList = LoadTexContextChunk( szChunkfilename );
				 if( !pTexContextList ) {
					 MessageBoxA( NULL, "Something went wrong. Couldn't parse the DB_TEXTURE_CONTEXT data-chunk file", "ERROR", MB_ICONERROR );
					 this->Close();
					 return;
				 }

				 pTexChunk = fopen( szTexchunkfname, "rb" );
				 pTexList = LoadTexChunk( pTexChunk );

				 for( INT i = 0; i < pTexContextList->iWrittenEntries; i++ ) {
					 if( !pTexContextList->context[i].iPacked ) continue;
					 listBox1->Items->Add( MakeString(pTexContextList->context[i].associatedMesh) );
				 }
			 }
	private: System::Void listBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
				 textBox2->Text = pTexContextList->context[listBox1->SelectedIndex+1].iTexNum.ToString();
				 textBox3->Text = MakeString( pTexContextList->context[listBox1->SelectedIndex+1].associatedMesh );
				 textBox4->Text = pTexContextList->context[listBox1->SelectedIndex+1].iFX.ToString();
				 if( pTextureBitmap ) delete pTextureBitmap;
				 pTextureBitmap = DrawTextureIntoABMP( pTexChunk, pTexList, pTexContextList->context[listBox1->SelectedIndex+1].iTexNum );
				 pictureBox1->Image = pTextureBitmap;
			 }

			 // Reassign the properties
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
			 pTexContextList->context[listBox1->SelectedIndex+1].iFX = Convert::ToInt32( textBox4->Text );
			 pTexContextList->context[listBox1->SelectedIndex+1].iTexNum = Convert::ToInt16( textBox2->Text );
			 free( pTexContextList->context[listBox1->SelectedIndex+1].associatedMesh );
			 pTexContextList->context[listBox1->SelectedIndex+1].associatedMesh = (char*)malloc( textBox3->Text->Length*4 );
			 strcpy( pTexContextList->context[listBox1->SelectedIndex+1].associatedMesh, (char*)Marshal::StringToHGlobalAnsi(textBox3->Text).ToPointer() );
		 }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 if( IDYES == MessageBoxA(NULL, "Are you sure you want to overwrite this data?",
				 "Saving changes", MB_ICONQUESTION|MB_YESNO) ) {
					 dbl_chunk_header_s* pHeader = (dbl_chunk_header_s*)malloc( sizeof(dbl_chunk_header_s) );

					 pHeader->iDataChunkType = DB_TEXTURE_CONTEXT;
					 pHeader->iDataChunkVer = 3;
					 pHeader->iFlag = 0x0E;

					 CreateTexContextChunk( (char*)Marshal::StringToHGlobalAnsi(textBox1->Text).ToPointer(), pTexContextList, pHeader );
					 free( pHeader );
			 }
		 }
private: System::Void wtexcontexted_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
			 fclose( pTexChunk );
			 free( szChunkfilename );
			 if( pTexList ) {
				free( pTexList->textures );
				free( pTexList );
			 }
			 if( pTextureBitmap ) delete pTextureBitmap;
			 if( pTexContextList ) {
				free( pTexContextList->context );
				free( pTexContextList );
			 }
			 GC::Collect();
		 }
};
}