// Ð¸Ð¼Ñ, id, ÐºÐ¾Ð¾Ñ€Ð´Ð¸Ð½Ð°Ñ‚Ñ‹ ÑƒÐ³Ð»Ð¾Ð², id Ð»Ð¾ÐºÐ°Ñ†Ð¸Ð¸
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Zone
{
    public uint id { get; set; }
    public uint location_id { get; set; }
    public string name { 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 GameObject buttons;

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

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

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

            foreach (var z in WallCreate.Zones)
            {
                list.AddRange(BitConverter.GetBytes(z.id));
                list.AddRange(BitConverter.GetBytes(z.start_width));
                list.AddRange(BitConverter.GetBytes(z.start_height));
                list.AddRange(BitConverter.GetBytes(z.end_width));
                list.AddRange(BitConverter.GetBytes(z.end_height));
                Debug.Log($"zone save id {z.id}  x {z.start_width} z {z.start_height} x2 {z.end_width} z2 {z.end_height}");
            }

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

    public static void ReceiveFromServer(byte[] bytedata)
    {
        int read = 5;
        var location = PlayerController.locations[PlayerController.active_company];
        location.zones = new List<Zone>();
        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($"zone received id {id} x1 {x1} z1 {z1} x2 {x2} z2 {z2} location {lid}");
            read += 24;

            location.zones.Add(new Zone { id = id, location_id = location.id, name = $"Ð—Ð¾Ð½Ð° {location.zones.Count + 1}", start_height = x1, start_width = z1, end_height = x2, end_width = z2 });
        }

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

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

