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

public class User
{
    public uint id { get; set; }
    public string name { get; set; }

    public static void UsersReceive(byte[] bytedata)
    {
        Debug.Log("UsersReceive");
        var player = PlayerController.instance;
        player.users = new List<User>();
        int read = 5;
        while (read < bytedata.Length)
        {
            var id = BitConverter.ToUInt32(bytedata, read);
            var lenname = bytedata[read + 4];
            var name = Encoding.UTF8.GetString(bytedata, read + 5, lenname);
            read += 5 + lenname;
            //print($"user received id {id} lenname {lenname} name {name}");
            player.users.Add(new User { id = id, name = name });
        }
        player.users_load = true;
    }

    public static void CoordinatesReceive(byte[] bytedata)
    {
        //var coordleng = bytedata.Length - 11;
        var accid = BitConverter.ToUInt32(bytedata, 5);
        var locid = BitConverter.ToUInt32(bytedata, 9);
        var player = PlayerController.instance;
        //WorkerController.TestStructures.Clear();
        //print("len " + BitConverter.ToUInt32(bytedata, 1));
        var worker = new List<Structure>();
        for (var read = 13; read < bytedata.Length; read += 20)
        {
            var id = BitConverter.ToUInt32(bytedata, read);
            var x = BitConverter.ToInt32(bytedata, read + 4);
            var y = BitConverter.ToInt32(bytedata, read + 8);
            var ticks = BitConverter.ToInt64(bytedata, read + 12);
            //print($"coord accid {accid} locid {locid} {id} x {x} y{y} ticks {ticks}");

            //WorkerController.markers.ElementAt(1).marker.transform.position = new Vector3(x, 0.5f, y); 

            //WorkerController.TestStructures
            worker.Add(new Structure
            {
                id = id,
                coord_x = x,
                coord_y = y,
                ts = new DateTime(ticks),
                acc_id = accid,
                location_id = locid,
                zone_id = 1
            });
        }
        player.Workers[accid] = worker;
        player.end_send[accid] = true;
    }
}