using GTANetworkAPI;
using Newtonsoft.Json;
using ReallifeGamemode.Server.Models;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Server.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

/**
* @overview Life of German Reallife - Entities User (User.cs)
* @author VegaZ, hydrant
* @copyright (c) 2008 - 2018 Life of German
*/

namespace ReallifeGamemode.Server.Entities
{
    public class User : IBankAccountOwner
    {
        [NotMapped]
        private int _wanteds;

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [StringLength(32)]
        public string Name { get; set; }
        [StringLength(32)]
        public string SocialClubName { get; set; }
        [StringLength(64)]
        public string Password { get; set; }
        public int LogUserId { get; set; }
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public DateTime RegistrationDate { get; set; }

        [EmailAddress]
        [StringLength(64)]
        public string Email { get; set; }
        public AdminLevel AdminLevel { get; set; }

        public bool Dead { get; set; }
        public int Handmoney { get; set; }

        public float PositionX { get; set; }
        public float PositionY { get; set; }
        public float PositionZ { get; set; }

        [ForeignKey("Character")]
        public int? CharacterId { get; set; }
        public Character Character { get; set; }

        [ForeignKey("Ban")]
        public int? BanId { get; set; }
        public Ban Ban { get; set; }

        public int? BusinessId { get; set; }

        public int? FactionId { get; set; }
        public Faction Faction { get; set; }

        public bool FactionLeader { get; set; }

        public int? FactionRankId { get; set; }
        public FactionRank FactionRank { get; set; }

        public Group Group { get; set; }

        public GroupRank GroupRank { get; set; }

        [ForeignKey("House")]
        public int? HouseId { get; set; }
        public House House { get; set; }

        public int? JobId { get; set; }

        public int Wanteds
        {
            get => _wanteds;
            set
            {
                this._wanteds = value;
                Client.TriggerEvent("SERVER:SetWanteds", value);
            }
        }
        
        public int Wage { get; set; }

        public int JailTime { get; set; }

        public bool IsAdmin(AdminLevel level) => AdminLevel >= level;

        public IBankAccount GetBankAccount(DatabaseContext databaseContext = null)
        {
            if (databaseContext == null)
            {
                using (databaseContext = new DatabaseContext())
                {
                    return databaseContext.UserBankAccounts.FirstOrDefault(u => u.UserId == this.Id);
                }
            }
            else
            {
                return databaseContext.UserBankAccounts.FirstOrDefault(u => u.UserId == this.Id);
            }
        }

        [NotMapped]
        public Client Client
        {
            get => NAPI.Pools.GetAllPlayers().Where(c => c.Name.ToLower() == this.Name.ToLower()).FirstOrDefault();
        }

        
    }
}
