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

public class Beacon
{
    public uint id { get; set; }
    public uint location_id { get; set; }
    public float x { get; set; }
    public float y { get; set; }
    public float z { get; set; } // этаж

    public ushort vagrant_label;
    public string uuid { get; set; } = "00000000-0000-0000-0000-000000000000";
    public ushort minor { get; set; }
    public ushort major { get; set; }
    public string mac { get; set; } = "00-00-00-00-00-00";
    public GameObject beacon { get; set; }
    public GameObject panel_button { get; set; }
    public bool enabled { get; set; }    
    public byte byteEnabled { 
        get
        {
            return enabled ? (byte) 1 : (byte) 0;
        }
    }

    public static void Save(List<Beacon> beacons)
    {
        if (beacons.Count > 0)
        {
            List<byte> list = new List<byte>();

            foreach (var b in beacons)
            {
                list.AddRange(BitConverter.GetBytes(b.id));
                list.AddRange(BitConverter.GetBytes(b.x));
                list.AddRange(BitConverter.GetBytes(b.y));
                list.AddRange(BitConverter.GetBytes(b.z));
                list.AddRange(BitConverter.GetBytes(b.minor));
                list.AddRange(BitConverter.GetBytes(b.major));                
                if (b.uuid.Length != 36)
                    b.uuid = "00000000-0000-0000-0000-000000000000";
                list.AddRange(Encoding.UTF8.GetBytes(b.uuid));
                Debug.Log($"beacon Save id {b.id} x {b.x} y {b.y} z {b.z} enabled {b.enabled} byte: {b.byteEnabled} loc_id {b.location_id}");
                list.Add(b.byteEnabled);
                list.AddRange(BitConverter.GetBytes(b.location_id));
                if (b.mac.Length != 17)
                    b.mac = "00-00-00-00-00-00";
                list.AddRange(Encoding.UTF8.GetBytes(b.mac));
            }

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

    //public static void BeaconsSaveAll()
    //{
    //    GameObject editor = GameObject.Find("Canvas").transform.GetChild(3).gameObject;
    //    if (editor != null)
    //    {
    //        var e2c = editor.GetComponent<EditorController>();
    //        var beacons = e2c.Beacons;
    //        if (beacons.Count > 0)
    //        {
    //            Beacon.Save(beacons);
    //        }
    //    }
    //}

    public static void ReceiveBeacons(byte[] bytedata)
    {
        int read = 5;
        int num = 0;
        var company = CompanyController.instance;
        uint location_id = 0;
        while (read < bytedata.Length)
        {
            num++;
            var id = BitConverter.ToUInt32(bytedata, read);
            var uuid = Encoding.UTF8.GetString(bytedata, read + 4, 36);
            var x = BitConverter.ToSingle(bytedata, read + 40);
            var y = BitConverter.ToSingle(bytedata, read + 44);
            var z = BitConverter.ToSingle(bytedata, read + 48);
            var minor = BitConverter.ToUInt16(bytedata, read + 52);
            var major = BitConverter.ToUInt16(bytedata, read + 54);
            var enabled = bytedata[read + 56];
            var lid = BitConverter.ToUInt32(bytedata, read + 57);
            var mac = Encoding.UTF8.GetString(bytedata, read + 61, 17);
            var label = BitConverter.ToUInt16(bytedata, read + 78);
            Debug.Log($"beacon received id {id} uuid {uuid} x {x} y {y} z {z} minor {minor} major {major} enabled {enabled} location {lid} mac{mac} label {label}");
            read += 80;

            //PlayerController.Beacons.Add(new Beacon { id = id, location_id = lid, uuid = uuid, x = x, y = y, z = z, minor = minor, major = major, enabled = enabled == 1 ? true : false });
            company.locations[lid].beacons.Add(new Beacon { vagrant_label = label, id = id, location_id = lid, uuid = uuid, x = x, y = y, z = z, minor = minor, major = major, mac = mac, enabled = enabled == 1 ? true : false });
            location_id = lid;
        }
        Debug.Log("Beacons loaded: " + num);
        //PlayerController.beacons_load = true;
        if (location_id != 0) company.load_location_elements[location_id].beacons = true;
    }

    public static void Request(uint locationId)
    {
        Debug.Log("BeaconsRequest connected " + Client.instance.connected + " locationId " + locationId);
        //if (!connected)
        //{            
        //    return;
        //}
        CompanyController.instance.locations[locationId].beacons = new List<Beacon>();
        Client.SendFourByteParamPacket(48, locationId);
    }
}