using GTANetworkAPI;
using ReallifeGamemode.Server.Entities;
using ReallifeGamemode.Server.Managers;
using ReallifeGamemode.Server.Models;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Server.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReallifeGamemode.Server.Job
{
    public abstract class JobBase
    {
        public delegate void JobStartHandler(Client player);
        public delegate void JobStopHandler(Client player);

        public event JobStartHandler JobStart;
        public event JobStopHandler JobStop;

        private readonly List<Client> _inJob = new List<Client>();

        public abstract int Id { get; }

        public abstract string Name { get; }

        public abstract bool NeedVehicleToStart { get; }

        public void StartJob(Client player)
        {
            if (_inJob.Contains(player)) return;
            _inJob.Add(player);

            ChatService.SendMessage(player, $"~y~[JOB]~s~ Du hast deinen Job ~o~{this.Name}~s~ gestartet.");

            JobStart?.Invoke(player);
        }

        public void StopJob(Client player, bool quit = false)
        {
            if (!_inJob.Contains(player)) return;
            _inJob.Remove(player);

            if (quit)
            {
                ChatService.SendMessage(player, $"~y~[JOB]~s~ Du hast deinen Job ~o~{this.Name}~s~ beendet.");
            }

            JobStop?.Invoke(player);
        }

        public List<JobVehicle> GetJobVehicles()
        {
            using (var dbContext = new DatabaseContext())
            {
                return dbContext.JobVehicles.Where(j => j.JobId == Id).ToList();
            }
        }

        public List<Client> GetUsersInJob() => _inJob;
    }
}
