#pragma once
#include "AboutForm.h"
#include "RegisterForm1.h"

namespace Project1 {

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


	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	protected: 
	public: System::Windows::Forms::ListBox^  lbCuttingOrder;
	public: System::Windows::Forms::ListBox^  lbUnusedPieces;
	public: System::Windows::Forms::ListBox^  lbUsedPieces;
	public: System::Windows::Forms::TextBox^  tbWaste;
	public: System::Windows::Forms::Button^  cbStart;

	public: System::Windows::Forms::Label^  Label7;
	public: System::Windows::Forms::Label^  Label6;
	public: System::Windows::Forms::Label^  Label5;
	public: System::Windows::Forms::Label^  Label1;
	private:	optimal1dx::Optimization1DX ^ Optimization1DX1;
	private: int iteration, stationary, bar;
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  fileToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  exitToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  helpToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  registerToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  aboutToolStripMenuItem;

	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->lbCuttingOrder = (gcnew System::Windows::Forms::ListBox());
			this->lbUnusedPieces = (gcnew System::Windows::Forms::ListBox());
			this->lbUsedPieces = (gcnew System::Windows::Forms::ListBox());
			this->tbWaste = (gcnew System::Windows::Forms::TextBox());
			this->cbStart = (gcnew System::Windows::Forms::Button());
			this->Label7 = (gcnew System::Windows::Forms::Label());
			this->Label6 = (gcnew System::Windows::Forms::Label());
			this->Label5 = (gcnew System::Windows::Forms::Label());
			this->Label1 = (gcnew System::Windows::Forms::Label());
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->exitToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->helpToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->registerToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->aboutToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// lbCuttingOrder
			// 
			this->lbCuttingOrder->BackColor = System::Drawing::SystemColors::Window;
			this->lbCuttingOrder->Cursor = System::Windows::Forms::Cursors::Default;
			this->lbCuttingOrder->Font = (gcnew System::Drawing::Font(L"Arial", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->lbCuttingOrder->ForeColor = System::Drawing::SystemColors::WindowText;
			this->lbCuttingOrder->ItemHeight = 14;
			this->lbCuttingOrder->Location = System::Drawing::Point(12, 235);
			this->lbCuttingOrder->Name = L"lbCuttingOrder";
			this->lbCuttingOrder->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->lbCuttingOrder->Size = System::Drawing::Size(354, 116);
			this->lbCuttingOrder->TabIndex = 26;
			// 
			// lbUnusedPieces
			// 
			this->lbUnusedPieces->BackColor = System::Drawing::SystemColors::Window;
			this->lbUnusedPieces->Cursor = System::Windows::Forms::Cursors::Default;
			this->lbUnusedPieces->Font = (gcnew System::Drawing::Font(L"Arial", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->lbUnusedPieces->ForeColor = System::Drawing::SystemColors::WindowText;
			this->lbUnusedPieces->ItemHeight = 14;
			this->lbUnusedPieces->Location = System::Drawing::Point(277, 67);
			this->lbUnusedPieces->Name = L"lbUnusedPieces";
			this->lbUnusedPieces->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->lbUnusedPieces->Size = System::Drawing::Size(89, 130);
			this->lbUnusedPieces->TabIndex = 22;
			// 
			// lbUsedPieces
			// 
			this->lbUsedPieces->BackColor = System::Drawing::SystemColors::Window;
			this->lbUsedPieces->Cursor = System::Windows::Forms::Cursors::Default;
			this->lbUsedPieces->Font = (gcnew System::Drawing::Font(L"Arial", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->lbUsedPieces->ForeColor = System::Drawing::SystemColors::WindowText;
			this->lbUsedPieces->ItemHeight = 14;
			this->lbUsedPieces->Location = System::Drawing::Point(148, 67);
			this->lbUsedPieces->Name = L"lbUsedPieces";
			this->lbUsedPieces->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->lbUsedPieces->Size = System::Drawing::Size(89, 130);
			this->lbUsedPieces->TabIndex = 21;
			// 
			// tbWaste
			// 
			this->tbWaste->AcceptsReturn = true;
			this->tbWaste->BackColor = System::Drawing::SystemColors::Window;
			this->tbWaste->Cursor = System::Windows::Forms::Cursors::IBeam;
			this->tbWaste->Font = (gcnew System::Drawing::Font(L"Arial", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->tbWaste->ForeColor = System::Drawing::SystemColors::WindowText;
			this->tbWaste->Location = System::Drawing::Point(12, 111);
			this->tbWaste->MaxLength = 0;
			this->tbWaste->Name = L"tbWaste";
			this->tbWaste->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->tbWaste->Size = System::Drawing::Size(97, 20);
			this->tbWaste->TabIndex = 19;
			// 
			// cbStart
			// 
			this->cbStart->BackColor = System::Drawing::SystemColors::Control;
			this->cbStart->Cursor = System::Windows::Forms::Cursors::Default;
			this->cbStart->Font = (gcnew System::Drawing::Font(L"Arial", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->cbStart->ForeColor = System::Drawing::SystemColors::ControlText;
			this->cbStart->Location = System::Drawing::Point(12, 48);
			this->cbStart->Name = L"cbStart";
			this->cbStart->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->cbStart->Size = System::Drawing::Size(81, 25);
			this->cbStart->TabIndex = 18;
			this->cbStart->Text = L"Start";
			this->cbStart->UseVisualStyleBackColor = false;
			this->cbStart->Click += gcnew System::EventHandler(this, &Form1::cbStart_Click);
			// 
			// Label7
			// 
			this->Label7->BackColor = System::Drawing::SystemColors::Control;
			this->Label7->Cursor = System::Windows::Forms::Cursors::Default;
			this->Label7->Font = (gcnew System::Drawing::Font(L"Arial", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->Label7->ForeColor = System::Drawing::SystemColors::ControlText;
			this->Label7->Location = System::Drawing::Point(9, 215);
			this->Label7->Name = L"Label7";
			this->Label7->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->Label7->Size = System::Drawing::Size(324, 17);
			this->Label7->TabIndex = 25;
			this->Label7->Text = L"Cutting Order (Length, Angle1, Angle2)";
			// 
			// Label6
			// 
			this->Label6->BackColor = System::Drawing::SystemColors::Control;
			this->Label6->Cursor = System::Windows::Forms::Cursors::Default;
			this->Label6->Font = (gcnew System::Drawing::Font(L"Arial", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->Label6->ForeColor = System::Drawing::SystemColors::ControlText;
			this->Label6->Location = System::Drawing::Point(274, 47);
			this->Label6->Name = L"Label6";
			this->Label6->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->Label6->Size = System::Drawing::Size(125, 17);
			this->Label6->TabIndex = 24;
			this->Label6->Text = L"Unused pieces (Length)";
			// 
			// Label5
			// 
			this->Label5->BackColor = System::Drawing::SystemColors::Control;
			this->Label5->Cursor = System::Windows::Forms::Cursors::Default;
			this->Label5->Font = (gcnew System::Drawing::Font(L"Arial", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->Label5->ForeColor = System::Drawing::SystemColors::ControlText;
			this->Label5->Location = System::Drawing::Point(145, 47);
			this->Label5->Name = L"Label5";
			this->Label5->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->Label5->Size = System::Drawing::Size(122, 17);
			this->Label5->TabIndex = 23;
			this->Label5->Text = L"Used Pieces (Length)";
			// 
			// Label1
			// 
			this->Label1->BackColor = System::Drawing::SystemColors::Control;
			this->Label1->Cursor = System::Windows::Forms::Cursors::Default;
			this->Label1->Font = (gcnew System::Drawing::Font(L"Arial", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->Label1->ForeColor = System::Drawing::SystemColors::ControlText;
			this->Label1->Location = System::Drawing::Point(12, 91);
			this->Label1->Name = L"Label1";
			this->Label1->RightToLeft = System::Windows::Forms::RightToLeft::No;
			this->Label1->Size = System::Drawing::Size(97, 17);
			this->Label1->TabIndex = 20;
			this->Label1->Text = L"Total Waste";
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->fileToolStripMenuItem, 
				this->helpToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(407, 24);
			this->menuStrip1->TabIndex = 29;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// fileToolStripMenuItem
			// 
			this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->exitToolStripMenuItem});
			this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
			this->fileToolStripMenuItem->Size = System::Drawing::Size(35, 20);
			this->fileToolStripMenuItem->Text = L"File";
			// 
			// exitToolStripMenuItem
			// 
			this->exitToolStripMenuItem->Name = L"exitToolStripMenuItem";
			this->exitToolStripMenuItem->Size = System::Drawing::Size(92, 22);
			this->exitToolStripMenuItem->Text = L"Exit";
			this->exitToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::exitToolStripMenuItem_Click);
			// 
			// helpToolStripMenuItem
			// 
			this->helpToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->registerToolStripMenuItem, 
				this->aboutToolStripMenuItem});
			this->helpToolStripMenuItem->Name = L"helpToolStripMenuItem";
			this->helpToolStripMenuItem->Size = System::Drawing::Size(40, 20);
			this->helpToolStripMenuItem->Text = L"Help";
			// 
			// registerToolStripMenuItem
			// 
			this->registerToolStripMenuItem->Name = L"registerToolStripMenuItem";
			this->registerToolStripMenuItem->Size = System::Drawing::Size(129, 22);
			this->registerToolStripMenuItem->Text = L"Register ...";
			this->registerToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::registerToolStripMenuItem_Click);
			// 
			// aboutToolStripMenuItem
			// 
			this->aboutToolStripMenuItem->Name = L"aboutToolStripMenuItem";
			this->aboutToolStripMenuItem->Size = System::Drawing::Size(129, 22);
			this->aboutToolStripMenuItem->Text = L"About ...";
			this->aboutToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::aboutToolStripMenuItem_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(407, 363);
			this->Controls->Add(this->lbCuttingOrder);
			this->Controls->Add(this->lbUnusedPieces);
			this->Controls->Add(this->lbUsedPieces);
			this->Controls->Add(this->tbWaste);
			this->Controls->Add(this->cbStart);
			this->Controls->Add(this->Label7);
			this->Controls->Add(this->Label6);
			this->Controls->Add(this->Label5);
			this->Controls->Add(this->Label1);
			this->Controls->Add(this->menuStrip1);
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->Text = L"Cut 1D X - example";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void cbStart_Click(System::Object^  sender, System::EventArgs^  e) {

  cbStart->Enabled = false;

  Optimization1DX1->BladeWidth = 0;
  Optimization1DX1->Width = 10;
  Optimization1DX1->RandomSeed = 1;
  Optimization1DX1->OptimizationLevel = 50;
  Optimization1DX1->OxRotationAllowed = 1;
  Optimization1DX1->OyRotationAllowed = 1;
  Optimization1DX1->OzRotationAllowed = 1;

  Optimization1DX1->NumberOfRepositoryPieces = 8;
  
  Optimization1DX1->SetRepositoryPiece(0, 10, 0);
  Optimization1DX1->SetRepositoryPiece(1, 10, 1);
  Optimization1DX1->SetRepositoryPiece(2, 10, 2);
  Optimization1DX1->SetRepositoryPiece(3, 10, 3);
  Optimization1DX1->SetRepositoryPiece(4, 10, 4);
  Optimization1DX1->SetRepositoryPiece(5, 10, 5);
  Optimization1DX1->SetRepositoryPiece(6, 10, 6);
  Optimization1DX1->SetRepositoryPiece(7, 10, 7);

  Optimization1DX1->NumberOfDemandPieces = 10;

  Optimization1DX1->SetDemandPiece(0, 2, 90, 90, 0);
  Optimization1DX1->SetDemandPiece(1, 2, 90, 90, 1);
  Optimization1DX1->SetDemandPiece(2, 2, 90, 90, 2);
  Optimization1DX1->SetDemandPiece(3, 2, 90, 90, 3);
  Optimization1DX1->SetDemandPiece(4, 2, 90, 90, 4);
  Optimization1DX1->SetDemandPiece(5, 7, 90, 90, 5);
  Optimization1DX1->SetDemandPiece(6, 7, 90, 90, 6);
  Optimization1DX1->SetDemandPiece(7, 7, 90, 90, 7);
  Optimization1DX1->SetDemandPiece(8, 7, 90, 90, 8);
  Optimization1DX1->SetDemandPiece(9, 7 , 90, 90, 9);

  bar = 0;

  lbUsedPieces->Items->Clear();
  lbUnusedPieces->Items->Clear();

  Optimization1DX1->Start();
	
			 }
private: System::Void aboutToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
				 AboutForm ^af = gcnew(AboutForm);
				 if (af->ShowDialog()== Windows::Forms::DialogResult::OK){
		 }
}
private: System::Void exitToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 Close();
		 }
private: System::Void registerToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
				 RegisterForm ^rf = gcnew(RegisterForm);
				 rf->tbComputerID->Text = Optimization1DX1->ComputerID;
				 if (rf->ShowDialog()== Windows::Forms::DialogResult::OK){
					 Optimization1DX1->UserName = rf->tbName->Text;
					 Optimization1DX1->SerialNumber = rf->tbSerialNumber->Text;
					 Optimization1DX1->RegisterOptimization1DX();
				 }

		 }
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
			 Optimization1DX1 = gcnew Optimization1DX;
			 Optimization1DX1->OnFinish += gcnew optimal1dx::IOptimization1DXEvents_OnFinishEventHandler(this, &Form1::Optimization1DX1_OnFinish);
		 }
private: void Optimization1DX1_OnFinish(void) 
	{
  cbStart->Enabled = true;

  tbWaste->Text = Convert::ToString(Optimization1DX1->TotalWaste);

  int usedlayout, Length;
  int demand_ID, repository_ID, demand_Index, repository_Index;

  for (int i = 0; i < Optimization1DX1->NumberOfDemandPieces; i++){
    Optimization1DX1->DemandPieceUtilized(i, usedlayout, demand_ID, repository_Index, repository_ID);
    double angle1, angle2;
    Optimization1DX1->GetDemandPiece(i, Length, angle1, angle2, demand_ID);
    if (usedlayout >= 0)
      lbUsedPieces->Items->Add(Convert::ToString(Length));
    else
      lbUnusedPieces->Items->Add(Convert::ToString(Length));
  }
  lbCuttingOrder->Items->Clear();
  for (int k = 0; k < Optimization1DX1->TotalNumberOfUtilizedRepositoryPieces; k++){
    String ^tmp = "Bar " + Convert::ToString(k) + ":";

    int num_utilized;
    double angle1,angle2;
    int rotatedOx, rotatedOy, rotatedOz;

    Optimization1DX1->GetUtilizedDemandPiece(k, 0, repository_Index, demand_Index, rotatedOx, rotatedOy, rotatedOz, repository_ID, demand_ID);
    
    Optimization1DX1->GetRepositoryPiece(repository_Index, Length, repository_ID);
    tmp += "(Length = " + Convert::ToString(Length) + ")";
    Optimization1DX1->GetNumberOfUtilizedDemandPieces(k, num_utilized, repository_Index, repository_ID);
    for (int i = 0; i < num_utilized; i++){
      Optimization1DX1->GetUtilizedDemandPiece(k, i, repository_Index, demand_Index, rotatedOx, rotatedOy, rotatedOz, repository_ID, demand_ID);
      Optimization1DX1->GetDemandPiece(demand_Index, Length, angle1, angle2, demand_ID);
      if (rotatedOx){
        angle1 = 180 - angle1;
        angle2 = 180 - angle2;
      }
      if (rotatedOy){
        double tmp = angle1;
        angle1 = 180 - angle2;
        angle2 = 180 - tmp;
      }
      if (rotatedOz){
        double tmp = angle1;
        angle1 = angle2;
        angle2 = tmp;
      }
      tmp += "(" + Convert::ToString(Length) + "," + Convert::ToString(angle1) + "," +Convert::ToString(angle2) + ")";
    }
	double w;
    Optimization1DX1->GetWaste(k, w, repository_Index, repository_ID); 
    tmp += "(Waste = " + Convert::ToString(w) + ")";
    lbCuttingOrder->Items->Add(tmp);
  }

		 }
};

}