// Ð¸Ð¼Ñ, 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 = ""; 
    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));
        list.AddRange(BitConverter.GetBytes(location_id));
        var bname = System.Text.Encoding.UTF8.GetBytes(name);
        var namelen = (ushort) bname.Length;
        list.AddRange(BitConverter.GetBytes(namelen));
        list.AddRange(bname);
        Debug.Log($"zone save id {id}  x {start_width} z {start_height} x2 {end_width} z2 {end_height} location_id {location_id} namelen {namelen} name {name}");

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

    public static void SaveAll(uint lid)
    {
        if (WallCreate.Zones.Count > 0)
        {
            List<byte> list = new List<byte>();

            foreach (var z in WallCreate.Zones[lid])
            {
                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));
                list.AddRange(BitConverter.GetBytes(z.location_id));
                var bname = System.Text.Encoding.UTF8.GetBytes(z.name);
                var namelen = (ushort)bname.Length;
                list.AddRange(BitConverter.GetBytes(namelen));
                list.AddRange(bname);
                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 ReceiveZones(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);
            var namelen = BitConverter.ToUInt16(bytedata, read + 24);
            Console.WriteLine($"namelen {namelen}");
            //var name = "";
            var name = $"Зона {company.locations[lid].zones.Count + 1}";
            if (namelen > 0)
            {
                name = System.Text.Encoding.UTF8.GetString(bytedata, read + 26, namelen);
            }

            Debug.Log($"zone received id {id} x1 {x1} z1 {z1} x2 {x2} z2 {z2} location {lid} name {name}");
            read += 26+namelen;       

            company.locations[lid].zones.Add(new Zone { id = id, location_id = lid, name = name, start_width = x1, start_height = z1, end_width= x2, end_height  = z2});
            location_id = lid;
        }
        if (location_id != 0)
        {
            Debug.Log($"location {location_id} zones count {company.locations[location_id].zones.Count}");
            company.load_location_elements[location_id].zones = true;
        }
    }

    public static void LoadByLocationId(uint locId)
    {
        CompanyController.instance.locations[locId].zones = new List<Zone>();
        Client.SendFourByteParamPacket(52, 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)));
        var x1 = start_width - (end_width * 10 / 2);
        coord.Add(x1); //x1
        var x2 = start_height - (end_height * 10 / 2);
        coord.Add(x2); //z1
        var z1 = start_width + (end_width * 10 / 2);
        coord.Add( z1); //x2
        var z2 = start_height + (end_height * 10 / 2);
        coord.Add(z2); //z2

        return coord;
    }
}

