using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

/// <summary>
/// Коробка
/// </summary>
public class Wall
{
    public uint id = 0;
    public uint location_id {get;set;}
    public float start_width { get; set; }
    public float start_height { get; set; }
    public float end_width { get; set; }
    public float end_height { get; set; }
    public GameObject go;

    public void Save()
    {
        List<byte> list = new List<byte>();
        list.AddRange(BitConverter.GetBytes(id));
        list.AddRange(BitConverter.GetBytes(start_width));
        list.AddRange(BitConverter.GetBytes(start_height));
        list.AddRange(BitConverter.GetBytes(end_width));
        list.AddRange(BitConverter.GetBytes(end_height));
        list.AddRange(BitConverter.GetBytes(location_id));
        Debug.Log($"wall save id {id}  x {start_width} z {start_height} x2 {end_width} z2 {end_height} location_id {location_id}");            

        var tosend = Client.ConstructVariablePacket(50, list.ToArray());
        Client.SendEnqueue(tosend);        
    }

    public static void SaveAll(uint lid)
    {
        Debug.Log("WallObjects count " + WallCreate.Walls.Count);
        if (WallCreate.Walls.Count > 0)
        {
            List<byte> list = new List<byte>();

            foreach (var w in WallCreate.Walls[lid])
            {
                list.AddRange(BitConverter.GetBytes(w.id));
                list.AddRange(BitConverter.GetBytes(w.start_width));
                list.AddRange(BitConverter.GetBytes(w.start_height));
                list.AddRange(BitConverter.GetBytes(w.end_width));
                list.AddRange(BitConverter.GetBytes(w.end_height));
                Debug.Log($"wall save id {w.id}  x {w.start_width} z {w.start_height} x2 {w.end_width} z2 {w.end_height}");
            }

            var tosend = Client.ConstructVariablePacket(50, list.ToArray());
            Client.SendEnqueue(tosend);
        }
    }

    public static void ReceiveWalls(byte[] bytedata)
    {
        int read = 5;
        var company = CompanyController.instance;
        uint location_id = 0;
        while (read < bytedata.Length)
        {
            var id = BitConverter.ToUInt32(bytedata, read);
            var x1 = BitConverter.ToSingle(bytedata, read + 4);
            var z1 = BitConverter.ToSingle(bytedata, read + 8);
            var x2 = BitConverter.ToSingle(bytedata, read + 12);
            var z2 = BitConverter.ToSingle(bytedata, read + 16);
            var lid = BitConverter.ToUInt32(bytedata, read + 20);
            Debug.Log($"wall received id {id} x1 {x1} z1 {z1} x2 {x2} z2 {z2}");
            read += 24;

            company.locations[lid].walls.Add(new Wall { id = id, location_id = lid, start_width = x1, start_height = z1, end_width = x2,  end_height= z2 });
            location_id = lid;
        }

        if (location_id != 0)
            company.load_location_elements[location_id].walls = true;
    }

    public static void LoadByLocationId(uint locId)
    { 
        CompanyController.instance.locations[locId].walls = new List<Wall>();
        Client.SendFourByteParamPacket(51, locId);       
    }

    /// <summary>
    /// Перевод позиции и масштаба в координаты левой нижней (x1;z1) и правой верхней (x2;z2) точек
    /// </summary>
    /// <returns></returns>
    public List<float> Coordinates()
    {
        var coord = new List<float>();

        //var wall = WallCreate.AddWall(this,
        //               new Vector3(w.start_width, WallCreate.WallHeight / 2, w.start_height),
        //               new Vector3(Mathf.Abs(w.end_width), WallCreate.WallHeight, Mathf.Abs(w.end_height)));
        coord.Add(end_width - (start_width/2));
        coord.Add(end_height - (start_height / 2));
        coord.Add(end_width + (start_width/2));
        coord.Add(end_height + (start_height / 2));

        return coord;
    }

    /// <summary>
    /// Перевод координат левой нижней (x1;z1) и правой верхней (x2;z2) точек
    /// в позицию и масштаба для Vector3
    /// </summary>
    /// <param name="x1"></param>
    /// <param name="z1"></param>
    /// <param name="x2"></param>
    /// <param name="z2"></param>
    /// <returns></returns>
    public List<float> Coordinates(float x1, float z1, float x2, float z2)
    {
        var coord = new List<float>();
        // scale
        var x = Math.Abs(x2 - x1);
        var z = Math.Abs(z2 - z1);
        coord.Add(x);
        coord.Add(z);
        // pos
        coord.Add(x/2);
        coord.Add(z/2);
        return coord;
    }

    public List<float> Coordinates(Vector3 pos, Vector3 scale)
    {
        var coord = new List<float>();

        //var wall = WallCreate.AddWall(this,
        //               new Vector3(w.start_width, WallCreate.WallHeight / 2, w.start_height),
        //               new Vector3(Mathf.Abs(w.end_width), WallCreate.WallHeight, Mathf.Abs(w.end_height)));
        coord.Add(scale.x - (pos.x / 2));
        coord.Add(scale.z - (pos.z / 2));
        coord.Add(scale.x + (pos.x / 2));
        coord.Add(scale.z + (pos.z / 2));

        return coord;
    }
}