using System;
using System.Collections.Generic;
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);       
    }
}