using System.Linq;
using GTANetworkAPI;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Extensions;

/**
* @overview Life of German Reallife - Event ResourceStop (ResourceStop.cs)
* @author VegaZ
* @copyright (c) 2008 - 2018 Life of German
*/

namespace ReallifeGamemode.Server.Events
{
  public class ResourceStop : Script
  {
    [ServerEvent(Event.ResourceStop)]
    public void OnResourceStop()
    {

      using (var dbContext = new DatabaseContext())
      {
        foreach (var player in NAPI.Pools.GetAllPlayers())
        {
          player.TriggerEvent("CLIENT:StopSound");

          User user = player.GetUser(dbContext);

          if (user == null)
          {
            continue;
          }

          user.PositionX = player.Position.X;
          user.PositionY = player.Position.Y;
          user.PositionZ = player.Position.Z;
          dbContext.SaveChanges();
        }
      }
    }
  }
}

