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));
        Debug.Log($"wall save id {id}  x {start_width} z {start_height} x2 {end_width} z2 {end_height}");            

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

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

            foreach (var w in WallCreate.WallObjects)
            {
                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 ReceiveFromServer(byte[] bytedata)
    {
        int read = 5;
        var location = PlayerController.locations[PlayerController.active_company];
        location.walls = new List<Wall>();
        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;

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

        PlayerController.load_location_elements[location.id].walls = true;
    }

    public static void LoadByLocationId(uint locId)
    {
        Client.SendFourByteParamPacket(51, locId);
    }
}