using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using optimal1dx;

namespace WebApplication2.Pages
{
    public class IndexModel : PageModel
    {
        public string Message { get; set; } = "Initial Request";

        static Optimization1DX Optimization1DX1;

        private readonly ILogger<IndexModel> _logger;

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
            Optimization1DX1 = new Optimization1DX();
            Optimization1DX1.OnFinish += new IOptimization1DXEvents_OnFinishEventHandler(Optimization1DX1_OnFinish);
            //Console.Write("Object created");
        }

        private void Optimization1DX1_OnFinish()
        {
            double waste = Optimization1DX1.TotalWaste;
            Message = "Waste = " + Convert.ToString(waste);

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

            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) + ")";
                Message += tmp;
            }
        }
        
        public void Start()
        {
            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);

            Optimization1DX1.Start();
        }

        public void OnGet(int id)
        {
            if (id == 3){
                Start();
            }
        }
    }
}
