using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NeonTea.Quakeball.Networking.Packets;
using NeonTea.Quakeball.TeaNet.Peers;
using NeonTea.Quakeball.TeaNet.Packets;

namespace NeonTea.Quakeball.Networking.Instances {
    public abstract class NetInstance {

        protected static byte[] Fingerprint = new byte[] { 0xFF, 0xF7 };

        public Peer Peer;
        public List<ulong> Connections = new List<ulong>();

        public Dictionary<ulong, NetPlayer> Players { get; private set; } = new Dictionary<ulong, NetPlayer>();
        public List<NetPlayer> PlayerList { get; private set; } = new List<NetPlayer>();
        public NetPlayer LocalPlayer;

        public NetInstance() {
            Peer = new Peer(Fingerprint);
        }

        public abstract void Start(string address, int port, string nick, PeerMessageListener listener);
        public abstract void OnStop();
        public abstract void Connected(Connection conn);
        public abstract void Disconnected(Connection conn);
        public abstract void Handle(Connection conn, Packet packet);

        public abstract void UpdateLocalPlayer();
        public abstract void SendPlayerSync();
        public abstract void LocalPlayerAction(PlayerAction action, Serializable serializable = null);

        public virtual void Update() { }

        protected void AddPlayer(NetPlayer player) {
            Players.Add(player.Id, player);
            PlayerList.Add(player);
        }

        protected void RemovePlayer(NetPlayer player) {
            Players.Remove(player.Id);
            PlayerList.Remove(player);
        }

        public void Stop() {
            if (Peer.Running) {
                OnStop();
                Peer.Stop();
                Peer.MessageListener.Message("Stopping");
            }
        }

    }
}