public async Task <UnitDetailsViewModel> PopulateNewUnitDataAsync(int JobId, int gender, int position)
        {
            UnitDetailsViewModel unit;
            Item   weaponItem1 = null, weaponItem2 = null, headItem = null, bodyItem = null;
            Random r = new Random();

            using (FFTContext context = new FFTContext())
            {
                Job unitJob = await context.Jobs.SingleAsync(m => m.JobId == JobId);

                int nameCount = context.UnitNames.Count(m => m.Gender == gender);

                unit = new UnitDetailsViewModel
                {
                    Unit = new UnitOverviewViewModel
                    {
                        UnitName = await context.UnitNames.Where(m => m.Gender == gender).Skip(r.Next(nameCount)).Select(m => m.Name).FirstAsync()
                    }
                };

                unit.Unit.JobID        = JobId;
                unit.Unit.JobName      = Enum.GetName(typeof(Enums.Jobs), JobId);
                unit.Unit.Gender       = gender;
                unit.Unit.GenderName   = Enum.GetName(typeof(Enums.Gender), gender);
                unit.Unit.Position     = position;
                unit.Unit.Level        = 1;
                unit.Unit.Experience   = r.Next(0, 99);
                unit.Unit.Brave        = r.Next(40, 70);
                unit.Unit.Faith        = r.Next(40, 70);
                unit.RawHP             = gender == 0 ? r.Next(491520, 524287) : gender == 1 ? r.Next(458752, 491519) : r.Next(573440, 622591);
                unit.RawMP             = gender == 0 ? r.Next(229376, 245759) : gender == 1 ? r.Next(245760, 262143) : r.Next(98304, 147455);
                unit.RawPhysicalAttack = gender == 0 ? 81920 : gender == 1 ? 65536 : r.Next(81920, 98303);
                unit.RawMagicalAttack  = gender == 0 ? 65536 : gender == 1 ? 81920 : r.Next(81920, 98303);
                unit.RawSpeed          = gender == 0 ? 98304 : gender == 1 ? 98304 : 81920;

                List <ItemCategoryViewModel> jobItems = await context.JobItems.Where(m => m.JobId == unit.Unit.JobID).Select(m => new ItemCategoryViewModel
                {
                    EquipmentCategoryID = m.ItemCategory.EquipmentCategoryId,
                    ItemCategoryID      = m.ItemCategoryId
                }).ToListAsync();

                if (jobItems.Any(m => m.EquipmentCategoryID == (int)Enums.EquipmentCategoriesList.Weapon))
                {
                    int randomWeaponID = jobItems.FirstOrDefault(m => m.EquipmentCategoryID == (int)Enums.EquipmentCategoriesList.Weapon).ItemCategoryID;
                    weaponItem1 = jobItems.Any(m => m.EquipmentCategoryID == (int)Enums.EquipmentCategoriesList.Weapon) ? context.Items.FirstOrDefault(m => m.ItemCategoryId ==
                                                                                                                                                       randomWeaponID && m.IsStartingItem) : null;
                    unit.WeaponID = weaponItem1 != null ? weaponItem1.ItemId : 0;
                    unit.ShieldID = JobId == (int)Enums.Jobs.Ninja ? unit.WeaponID : 0;
                }

                if (jobItems.Any(m => m.EquipmentCategoryID == (int)Enums.EquipmentCategoriesList.Shield))
                {
                    int randomShieldID = jobItems.FirstOrDefault(m => m.EquipmentCategoryID == (int)Enums.EquipmentCategoriesList.Shield).ItemCategoryID;
                    weaponItem2 = jobItems.Any(m => m.EquipmentCategoryID == (int)Enums.EquipmentCategoriesList.Shield) ? context.Items.FirstOrDefault(m => m.ItemCategoryId ==
                                                                                                                                                       randomShieldID && m.IsStartingItem) : null;
                    unit.ShieldID = weaponItem2 != null ? weaponItem2.ItemId : 0;
                }

                if (jobItems.Any(m => m.EquipmentCategoryID == (int)Enums.EquipmentCategoriesList.Helmet))
                {
                    int randomHeadID = jobItems.FirstOrDefault(m => m.EquipmentCategoryID == (int)Enums.EquipmentCategoriesList.Helmet).ItemCategoryID;
                    headItem = jobItems.Any(m => m.EquipmentCategoryID == (int)Enums.EquipmentCategoriesList.Helmet) ? context.Items.Where(c => c.ItemCategoryId == randomHeadID)
                               .FirstOrDefault(m => m.IsStartingItem) : null;
                    unit.HeadID = headItem != null ? headItem.ItemId : 0;
                }

                if (jobItems.Any(m => m.EquipmentCategoryID == (int)Enums.EquipmentCategoriesList.Armor))
                {
                    int randomBodyID = jobItems.FirstOrDefault(m => m.EquipmentCategoryID == (int)Enums.EquipmentCategoriesList.Armor).ItemCategoryID;
                    bodyItem = jobItems.Any(m => m.EquipmentCategoryID == (int)Enums.EquipmentCategoriesList.Armor) ? context.Items.FirstOrDefault(m => m.ItemCategoryId ==
                                                                                                                                                   randomBodyID && m.IsStartingItem) : null;
                    unit.BodyID = bodyItem != null ? bodyItem.ItemId : 0;
                }

                unit.Resistances = new UnitResistAndImmunityViewModel();
                unit             = AttributeCalculatorService.CalculateHPAndMP(headItem, bodyItem, unit, unitJob);
                unit             = AttributeCalculatorService.CalculateBasicStats(weaponItem1, weaponItem2, headItem, bodyItem, null, unit, unitJob);
                unit             = AttributeCalculatorService.CalculateEvasionStats(weaponItem2, null, unitJob, unit);
                unit             = AttributeCalculatorService.CalculateResistancesAndImmunities(new List <Item> {
                    weaponItem1, weaponItem2, headItem, bodyItem
                }, unit);
                unit.PrimaryAbilityJobID = unit.Unit.JobID;
                unit.PrimaryAbilityName  = unitJob.AbilitySetPspName;
            }

            return(unit);
        }
        public async Task <UnitDetailsViewModel> GetUnitStatsDetailDataAsync(UnitDetailsViewModel unit, int?ItemId = null)
        {
            Item        weaponItem1 = null, weaponItem2 = null, headItem = null, bodyItem = null, accessoryItem = null;
            List <Item> items = new List <Item>();

            unit.Unit.JobName         = Enum.GetName(typeof(Enums.Jobs), unit.Unit.JobID);
            unit.Unit.JobName         = unit.Unit.JobName.Contains("Onion") ? "Onion Knight" : unit.Unit.JobName;
            unit.Unit.JobPortraitPath = String.Format("/img/Jobs/{0}_{1}_Portrait.png", unit.Unit.JobName.Contains("Onion") ? "OnionKnight" : unit.Unit.JobName, unit.Unit.GenderName);
            unit.Unit.JobName         = string.Concat(unit.Unit.JobName.Select(m => Char.IsUpper(m) ? " " + m : m.ToString())).Trim();
            unit.Unit.GenderName      = Enum.GetName(typeof(Enums.Gender), unit.Unit.Gender);

            using (FFTContext context = new FFTContext())
            {
                if (ItemId != null)
                {
                    int  categoryListLength = Enum.GetNames(typeof(Enums.EquipmentCategoriesList)).Length;
                    Item selectedItem       = await context.Items.SingleAsync(m => m.ItemId == ItemId);

                    switch ((Enums.EquipmentCategoriesList)selectedItem.ItemCategory.EquipmentCategoryId)
                    {
                    case Enums.EquipmentCategoriesList.Weapon:
                        unit.WeaponID = selectedItem.ItemId;
                        break;

                    case Enums.EquipmentCategoriesList.Shield:
                        unit.ShieldID = selectedItem.ItemId;
                        break;

                    case Enums.EquipmentCategoriesList.Helmet:
                        unit.HeadID = selectedItem.ItemId;
                        break;

                    case Enums.EquipmentCategoriesList.Armor:
                        unit.BodyID = selectedItem.ItemId;
                        break;

                    case Enums.EquipmentCategoriesList.Accessory:
                        unit.AccessoryID = selectedItem.ItemId;
                        break;
                    }
                }

                if (unit.WeaponID != default(int))
                {
                    weaponItem1 = await context.Items.SingleAsync(m => m.ItemId == unit.WeaponID);

                    unit.WeaponRight = new ItemOverviewViewModel
                    {
                        WeaponPower         = weaponItem1.ItemCategory.EquipmentCategoryId == (int)Enums.EquipmentCategoriesList.Weapon ? weaponItem1.AttackPower.Value.ToString("D3") : "000",
                        WeaponHit           = weaponItem1.ItemCategory.EquipmentCategoryId == (int)Enums.EquipmentCategoriesList.Weapon ? weaponItem1.HitPercentage.Value.ToString("D3") : "000",
                        ShieldPhysicalEvade = weaponItem1.ItemCategory.EquipmentCategoryId == (int)Enums.EquipmentCategoriesList.Shield ? weaponItem1.PhysicalEvade.Value.ToString("D2") : "00",
                        ShieldMagicalEvade  = weaponItem1.ItemCategory.EquipmentCategoryId == (int)Enums.EquipmentCategoriesList.Shield ? weaponItem1.MagicalEvade.Value.ToString("D2") : "00",
                        PhysicalAttackPower = weaponItem1.PhysicalAttackBoost.HasValue ? weaponItem1.PhysicalAttackBoost.Value.ToString("D2") : "00",
                        MagicalAttackPower  = weaponItem1.MagicAttackBoost.HasValue ? weaponItem1.MagicAttackBoost.Value.ToString("D2") : "00",
                        ImagePath           = weaponItem1.ItemCategory.EquipmentCategoryId == (int)Enums.EquipmentCategoriesList.Weapon ? @"/img/Item_Icons/Weapons/" +
                                              weaponItem1.IconFileName : @"/img/Item_Icons/Armour/" + weaponItem1.IconFileName,
                        Name = weaponItem1.PspName,
                    };

                    items.Add(weaponItem1);
                }
                else
                {
                    unit.WeaponRight = new ItemOverviewViewModel();
                }

                if (unit.ShieldID != default && weaponItem1.ItemCategory.IsTwoHandOnly)
                {
                    weaponItem2 = await context.Items.SingleAsync(m => m.ItemId == unit.ShieldID);

                    unit.WeaponLeft = new ItemOverviewViewModel
                    {
                        WeaponPower = weaponItem2 != null ? weaponItem2.ItemCategory.EquipmentCategoryId == (int)Enums.EquipmentCategoriesList.Weapon ?
                                      weaponItem2.AttackPower.HasValue ? weaponItem2.AttackPower.Value.ToString("D3") : "000" : "000" : "000",
                        WeaponHit = weaponItem2 != null ? weaponItem2.ItemCategory.EquipmentCategoryId == (int)Enums.EquipmentCategoriesList.Weapon ?
                                    weaponItem2.HitPercentage.HasValue ? weaponItem2.HitPercentage.Value.ToString("D3") : "000" : "000" : "000",
                        ShieldPhysicalEvade = weaponItem2 != null ? weaponItem2.ItemCategory.EquipmentCategoryId == (int)Enums.EquipmentCategoriesList.Shield ?
                                              weaponItem2.PhysicalEvade.HasValue ? weaponItem2.PhysicalEvade.Value.ToString("D3") : "00" : "00" : "00",
                        ShieldMagicalEvade = weaponItem2 != null ? weaponItem2.ItemCategory.EquipmentCategoryId == (int)Enums.EquipmentCategoriesList.Shield ?
                                             weaponItem2.MagicalEvade.HasValue ? weaponItem2.MagicalEvade.Value.ToString("D3") : "00" : "00" : "00",
                        PhysicalAttackPower = weaponItem2 != null?weaponItem2.PhysicalAttackBoost.HasValue?weaponItem2.PhysicalAttackBoost.Value.ToString("D2") : "00" : "00",
                                                  MagicalAttackPower = weaponItem2 != null?weaponItem2.MagicAttackBoost.HasValue?weaponItem2.MagicAttackBoost.Value.ToString("D2") : "00" : "00",
                                                                           ImagePath = weaponItem2 != null ? weaponItem2.ItemCategory.EquipmentCategoryId == (int)Enums.EquipmentCategoriesList.Weapon ? @"/img/Item_Icons/Weapons/" +
                                                                                       weaponItem2.IconFileName : @"/img/Item_Icons/Armour/" + weaponItem2.IconFileName : "",
                                                                           Name = weaponItem2 != null ? weaponItem2.PspName : "",
                    };

                    items.Add(weaponItem2);
                }
                else
                {
                    unit.WeaponLeft = new ItemOverviewViewModel();
                }

                if (unit.HeadID != default(int))
                {
                    headItem = await context.Items.SingleAsync(m => m.ItemId == unit.HeadID);

                    unit.Head = new ItemOverviewViewModel
                    {
                        HPBonus             = headItem.HPBonus ?? 0,
                        MPBonus             = headItem.MPBonus ?? 0,
                        PhysicalAttackPower = headItem.PhysicalAttackBoost.HasValue ? headItem.PhysicalAttackBoost.Value.ToString("D2") : "00",
                        MagicalAttackPower  = headItem.MagicAttackBoost.HasValue ? headItem.MagicAttackBoost.Value.ToString("D2") : "00",
                        SpeedBonus          = headItem.SpeedBoost ?? 0,
                        ImagePath           = @"/img/Item_Icons/Armour/" + headItem.IconFileName,
                        Name = headItem.PspName,
                    };

                    items.Add(headItem);
                }
                else
                {
                    unit.Head = new ItemOverviewViewModel();
                }

                if (unit.BodyID != default(int))
                {
                    bodyItem = await context.Items.SingleAsync(m => m.ItemId == unit.BodyID);

                    unit.Body = new ItemOverviewViewModel
                    {
                        HPBonus             = bodyItem.HPBonus ?? 0,
                        MPBonus             = bodyItem.MPBonus ?? 0,
                        PhysicalAttackPower = bodyItem.PhysicalAttackBoost.HasValue ? bodyItem.PhysicalAttackBoost.Value.ToString("D2") : "00",
                        MagicalAttackPower  = bodyItem.MagicAttackBoost.HasValue ? bodyItem.MagicAttackBoost.Value.ToString("D2") : "00",
                        SpeedBonus          = bodyItem.SpeedBoost ?? 0,
                        ImagePath           = @"/img/Item_Icons/Armour/" + context.Items.Single(m => m.ItemId == unit.BodyID).IconFileName,
                        Name = context.Items.Single(m => m.ItemId == unit.BodyID).PspName,
                    };

                    items.Add(bodyItem);
                }
                else
                {
                    unit.Body = new ItemOverviewViewModel();
                }

                if (unit.AccessoryID != default(int))
                {
                    accessoryItem = await context.Items.SingleAsync(m => m.ItemId == unit.AccessoryID);

                    unit.Accessory = new ItemOverviewViewModel
                    {
                        ItemCategoryID         = accessoryItem.ItemCategoryId,
                        MoveBonus              = accessoryItem.MoveBoost ?? 0,
                        JumpBonus              = accessoryItem.JumpBoost ?? 0,
                        SpeedBonus             = accessoryItem.SpeedBoost ?? 0,
                        PhysicalAttackPower    = accessoryItem.PhysicalAttackBoost.HasValue ? accessoryItem.PhysicalAttackBoost.Value.ToString() : "00",
                        MagicalAttackPower     = accessoryItem.MagicAttackBoost.HasValue ? accessoryItem.MagicAttackBoost.Value.ToString() : "00",
                        AccessoryPhysicalEvade = accessoryItem.ItemCategory.EquipmentCategoryId == (int)Enums.EquipmentCategoriesList.Accessory ? accessoryItem.PhysicalEvade.HasValue ?
                                                 accessoryItem.PhysicalEvade.Value.ToString("D3") : "00" : "00",
                        AccessoryMagicalEvade = accessoryItem.ItemCategory.EquipmentCategoryId == (int)Enums.EquipmentCategoriesList.Accessory ? accessoryItem.MagicalEvade.HasValue ?
                                                accessoryItem.MagicalEvade.Value.ToString("D3") : "00" : "00",
                        ImagePath = @"/img/Item_Icons/Accessories/" + context.Items.Single(m => m.ItemId == unit.AccessoryID).IconFileName,
                        Name      = context.Items.Single(m => m.ItemId == unit.AccessoryID).PspName,
                    };

                    items.Add(accessoryItem);
                }
                else
                {
                    unit.Accessory = new ItemOverviewViewModel();
                }

                Job unitJob = await context.Jobs.SingleAsync(m => m.JobId == unit.Unit.JobID);

                unit = AttributeCalculatorService.CalculateHPAndMP(headItem, bodyItem, unit, unitJob);
                unit = AttributeCalculatorService.CalculateBasicStats(weaponItem1, weaponItem2, headItem, bodyItem, accessoryItem, unit, unitJob);
                unit = AttributeCalculatorService.CalculateEvasionStats(weaponItem2, accessoryItem, unitJob, unit);
                unit.PrimaryAbilityJobID = unitJob.JobId;
                unit.PrimaryAbilityName  = unitJob.AbilitySetPspName;
            }

            unit.Unit.AttributeDigits = new UnitOverviewHpMpViewModel
            {
                HpDigits         = unit.Unit.MaxHP.ToString().ToCharArray(),
                MpDigits         = unit.Unit.MaxMP.ToString().ToCharArray(),
                ExperienceDigits = unit.Unit.Experience < 10 ? ("0" + unit.Unit.Experience.ToString()).ToCharArray() : unit.Unit.Experience.ToString().ToCharArray(),
                LevelDigits      = unit.Unit.Level < 10 ? ("0" + unit.Unit.Level.ToString()).ToCharArray() : unit.Unit.Level.ToString().ToCharArray(),
                PositionDigits   = unit.Unit.Position < 10 ? ("0" + unit.Unit.Position.ToString()).ToCharArray() : unit.Unit.Position.ToString().ToCharArray()
            };

            unit = AttributeCalculatorService.CalculateResistancesAndImmunities(items, unit);
            return(unit);
        }