using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using optimal1dx;


namespace Opt1D_CD
{
    public partial class Form1 : Form
    {
        Optimization1DX Optimization1DX1;
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Optimization1DX1 = new Optimization1DX();
         	Optimization1DX1.OnFinish += new IOptimization1DXEvents_OnFinishEventHandler(Optimization1DX1_OnFinish);
        }

        private void Optimization1DX1_OnFinish() 
	    {
            cbStart.Enabled = true;

            tbWaste.Text = Convert.ToString(Optimization1DX1.TotalWaste);

            int usedlayout, Length;
            int demand_ID, demand_Index, repository_ID, repository_Index;
            double angle1, angle2;

            for (int i = 0; i < Optimization1DX1.NumberOfDemandPieces; i++){
                Optimization1DX1.DemandPieceUtilized(i, out usedlayout, out demand_ID,out  repository_Index, out repository_ID);
                double a1, a2;
                Optimization1DX1.GetDemandPiece(i, out Length, out angle1,out angle2, out 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;
                
                int rotatedOx, rotatedOy, rotatedOz;
                Optimization1DX1.GetUtilizedDemandPiece(k, 0, out repository_Index, out demand_Index, out rotatedOx, out rotatedOy, out rotatedOz, out repository_ID, out demand_ID);

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

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cbStart_Click(object sender, 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);

            lbUsedPieces.Items.Clear();
            lbUnusedPieces.Items.Clear();

            Optimization1DX1.Start();
        }

        private void registerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RegisterForm1 rf = new RegisterForm1();
		    rf.tbComputerID.Text = Optimization1DX1.ComputerID;
			if (rf.ShowDialog()== DialogResult.OK){
				 Optimization1DX1.UserName = rf.tbName.Text;
				 Optimization1DX1.SerialNumber = rf.tbSerialNumber.Text;
				 Optimization1DX1.RegisterOptimization1DX();
			}
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutForm af = new AboutForm();
		    if (af.ShowDialog()== DialogResult.OK){
		    }
        }
    }
}