using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using parkMonitor.DB;
using parkMonitor.DBLocation;
using System.Configuration;
using parkMonitor.entity;

namespace parkMonitor.server.CoreThread
{
    public class AllotParkingSpace
    {
        //根据车位位置及状态分配目标车位，返回Parking Space的ID
        public Parking_Space MallocParkingSpace(CEntrance pt_Ent,object c,Command queueCmd)
        {
            //DBOperation o = new DBOperation();
            List<Parking_Space> lps = null;
            if (c.GetType() == typeof(DBOperation))
            {
                DBOperation oper = (DBOperation)c;
                lps = oper.GetParkingSpace(queueCmd.garageID);
            }
            else
            {
                DBLocationOperator locationOper = (DBLocationOperator)c;
                lps = locationOper.GetParkingSpace(queueCmd.garageID);
            }
            double xWeight = Convert.ToDouble(ConfigurationManager.AppSettings["xWeight"]);
            double yWeight = Convert.ToDouble(ConfigurationManager.AppSettings["yWeight"]);
            double zWeight = Convert.ToDouble(ConfigurationManager.AppSettings["zWeight"]);

            Parking_Space rps = new Parking_Space();

            float min_dis = 10000;
            for (int i = 0; i < lps.Count(); i++)
            {
                float deltaX = (lps[i].parkingSpaceX - pt_Ent.parkingEntX) * (float)xWeight;
                float deltaY = (lps[i].parkingSpaceY - pt_Ent.parkingEntY) * (float)yWeight;
                float deltaZ = (lps[i].parkingSpaceZ - pt_Ent.parkingEntZ) * (float)zWeight;
                float d = (float)System.Math.Sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
                if (d < min_dis)
                {
                    min_dis = d;
                    rps = lps[i];
                }

            }
            return rps;
        }
    }

    public class CEntrance
    {
        public int parkingEntX { get; set; }
        public int parkingEntY { get; set; }
        public int parkingEntZ { get; set; }
    }
}
