public async Task <IActionResult> InvestInBusiness(int investingBusinessId, int investedBusinessId, double investmentAmount)
        {
            try
            {
                if (investingBusinessId == investedBusinessId)
                {
                    return(StatusCode(400, "You cannot invest in yourself"));
                }

                var investingBusiness = await _context.Business.SingleAsync(s => s.Id == investingBusinessId);

                var investedBusiness = await _context.Business.SingleAsync(s => s.Id == investedBusinessId);

                if (investedBusiness.LifeTimeEarnings < 1000000)
                {
                    return(StatusCode(400, "Cannot invest until you have earned 1,000,000 lifetime"));
                }
                if (investmentAmount <= 0 || investmentAmount > investingBusiness.CashPerSecond)
                {
                    return(StatusCode(400, "You do not have enough to invest"));
                }

                var investment = new Investment()
                {
                    InvestmentAmount                          = investmentAmount,
                    InvestmentExpiration                      = DateTime.UtcNow.AddDays(1),
                    InvestedBusinessCashAtInvestment          = investedBusiness.Cash,
                    InvestedBusinessCashPerSecondAtInvestment = investedBusiness.CashPerSecond,
                    InvestmentType = InvestmentType.Investment,
                };
                var investorBusinessInvestment = new BusinessInvestment()
                {
                    Investment          = investment,
                    InvestmentDirection = InvestmentDirection.Investor,
                    InvestmentType      = InvestmentType.Investment
                };
                var investeeBusinessInvestment = new BusinessInvestment()
                {
                    Investment          = investment,
                    InvestmentDirection = InvestmentDirection.Investee,
                    InvestmentType      = InvestmentType.Investment
                };

                investingBusiness.BusinessInvestments.Add(investorBusinessInvestment);
                investedBusiness.BusinessInvestments.Add(investeeBusinessInvestment);

                investingBusiness.CashPerSecond -= investmentAmount;
                investedBusiness.CashPerSecond  += investmentAmount;

                _context.Business.Update(investingBusiness);
                _context.Business.Update(investedBusiness);
                await _applicationHelper.TrySaveChangesConcurrentAsync(_context);

                return(StatusCode(200));
            }
            catch { return(StatusCode(500)); }
        }
        public static double CalculateInvestmentProfit(BusinessInvestment investment)
        {
            var investmentPercentage = (double)(investment.Investment.InvestmentAmount / investment.Investment.InvestedBusinessCashPerSecondAtInvestment);
            if (investmentPercentage > 1) investmentPercentage = 1;
            var profitSinceInvestment = (investment.Business.Cash - investment.Investment.InvestedBusinessCashAtInvestment);
            var profit = 0.00;
            if (profitSinceInvestment > 0) profit = profitSinceInvestment * investmentPercentage;

            return profit;
        }
        public async Task <IActionResult> InvestInCompany(int companyToInvestInId, float investmentAmount)
        {
            var user = await GetCurrentEntrepreneur();

            if (companyToInvestInId == user.Business.Id)
            {
                return(RedirectToAction("Index", "Business", new { id = companyToInvestInId }));                                         // cannot invest in yourself
            }
            if (user.Business.LifeTimeEarnings < 1000000)
            {
                return(RedirectToAction("Index", "Business", new { id = companyToInvestInId }));
            }
            if (investmentAmount <= 0 || investmentAmount > user.Business.CashPerSecond)
            {
                return(RedirectToAction("Index", "Business", new { id = companyToInvestInId }));
            }
            var companyToInvestIn = await _context.Business.SingleOrDefaultAsync(s => s.Id == companyToInvestInId);

            var investment = new Investment()
            {
                InvestmentAmount                          = investmentAmount,
                InvestmentExpiration                      = DateTime.UtcNow.AddDays(1),
                InvestedBusinessCashAtInvestment          = companyToInvestIn.Cash,
                InvestedBusinessCashPerSecondAtInvestment = companyToInvestIn.CashPerSecond,
                InvestmentType = InvestmentType.Investment,
            };
            var investorBusinessInvestment = new BusinessInvestment()
            {
                Investment          = investment,
                InvestmentDirection = InvestmentDirection.Investor,
                InvestmentType      = InvestmentType.Investment
            };
            var investeeBusinessInvestment = new BusinessInvestment()
            {
                Investment          = investment,
                InvestmentDirection = InvestmentDirection.Investee,
                InvestmentType      = InvestmentType.Investment
            };

            user.Business.BusinessInvestments.Add(investorBusinessInvestment);
            companyToInvestIn.BusinessInvestments.Add(investeeBusinessInvestment);

            user.Business.CashPerSecond     -= investmentAmount;
            companyToInvestIn.CashPerSecond += investmentAmount;

            _context.Business.Update(user.Business);
            _context.Business.Update(companyToInvestIn);
            await _applicationHelper.TrySaveChangesConcurrentAsync(_context);

            return(RedirectToAction("Index", "Business", new { id = companyToInvestInId }));
        }
        public async Task <IActionResult> EspionageBusiness(int attackingBusinessId, int defendingBusinessId)
        {
            var attackingBusiness = await _context.Business.SingleOrDefaultAsync(s => s.Id == attackingBusinessId);

            var companyToEspionage = await _context.Business.SingleOrDefaultAsync(s => s.Id == defendingBusinessId);

            if (companyToEspionage.AmountEmployed < 70)
            {
                return(StatusCode(400, "Cannot espionage until you have 70 employeess"));
            }
            if (attackingBusiness.Cash < attackingBusiness.EspionageCost)
            {
                return(StatusCode(400, "You do not have enough cash to commit this espionage"));
            }
            if (attackingBusiness.Id == defendingBusinessId)
            {
                return(StatusCode(400, "You cannot espionage yourself"));
            }

            attackingBusiness.Cash -= attackingBusiness.EspionageCost;
            _context.Business.Update(attackingBusiness);
            await _context.SaveChangesAsync();

            var rand = new Random();

            if (((attackingBusiness.EspionageChance * 100) - (companyToEspionage.EspionageDefense * 100)) < rand.Next(0, 100))
            {
                return(StatusCode(200, "Unsuccessful Espionage"));
            }

            var espionageAmountPercentage = CalculateEspionagePercentage(companyToEspionage, attackingBusiness);
            var espionageAmount           = companyToEspionage.CashPerSecond * espionageAmountPercentage;

            var investment = new Investment()
            {
                InvestmentAmount     = (espionageAmount),
                InvestmentExpiration = DateTime.UtcNow.AddDays(1),
                InvestmentType       = InvestmentType.Espionage,
            };
            var investorBusinessInvestment = new BusinessInvestment()
            {
                InvestmentType      = InvestmentType.Espionage,
                Investment          = investment,
                InvestmentDirection = InvestmentDirection.Investor,
            };
            var investeeBusinessInvestment = new BusinessInvestment()
            {
                InvestmentType      = InvestmentType.Espionage,
                Investment          = investment,
                InvestmentDirection = InvestmentDirection.Investee,
            };

            companyToEspionage.CashPerSecond     = espionageAmount;
            companyToEspionage.EspionageDefense += .05F;

            attackingBusiness.BusinessInvestments.Add(investorBusinessInvestment);
            companyToEspionage.BusinessInvestments.Add(investeeBusinessInvestment);
            _context.Business.Update(companyToEspionage);
            _context.Business.Update(attackingBusiness);
            await _context.SaveChangesAsync();

            return(StatusCode(200, "Successful Espionage!"));
        }
        public async Task <IActionResult> CommitEspionage(int companyToEspionageId)
        {
            double CalculateEspionageCost(Business business)
            {
                var cost = business.Cash * 0.01;

                if (cost < 10000)
                {
                    cost = 10000;
                }
                return(cost);
            }

            var user = await GetCurrentEntrepreneur();

            var companyToEspionage = await _context.Business.SingleOrDefaultAsync(s => s.Id == companyToEspionageId);

            var costOfEspionage = CalculateEspionageCost(user.Business);

            if (companyToEspionage.AmountEmployed < 70)
            {
                return(RedirectToAction("Index", "Business", new { id = companyToEspionageId }));
            }
            if (user.Business.Cash < costOfEspionage)
            {
                return(RedirectToAction("Index", "Business", new { id = companyToEspionageId }));
            }
            if (user.Business.Id == companyToEspionageId)
            {
                return(RedirectToAction("Index", "Business", new { id = companyToEspionageId }));
            }

            user.Business.Cash -= costOfEspionage;
            _context.Business.Update(user.Business);
            await _context.SaveChangesAsync();

            var rand = new Random();

            if (((user.Business.EspionageChance * 100) - (companyToEspionage.EspionageDefense * 100)) < rand.Next(0, 100))
            {
                return(Ok(JsonConvert.SerializeObject(new { SuccessfulEspionage = false, EspionageAmount = 0, UpdatedEspionageCost = CalculateEspionageCost(user.Business) })));
            }

            var espionageAmountPercentage = CalculateEspionagePercentage(companyToEspionage, user.Business);
            var espionageAmount           = companyToEspionage.CashPerSecond * espionageAmountPercentage;

            var investment = new Investment()
            {
                InvestmentAmount     = (espionageAmount),
                InvestmentExpiration = DateTime.UtcNow.AddDays(1),
                InvestmentType       = InvestmentType.Espionage,
            };
            var investorBusinessInvestment = new BusinessInvestment()
            {
                InvestmentType      = InvestmentType.Espionage,
                Investment          = investment,
                InvestmentDirection = InvestmentDirection.Investor,
            };
            var investeeBusinessInvestment = new BusinessInvestment()
            {
                InvestmentType      = InvestmentType.Espionage,
                Investment          = investment,
                InvestmentDirection = InvestmentDirection.Investee,
            };

            companyToEspionage.CashPerSecond     = espionageAmount;
            companyToEspionage.EspionageDefense += .05F;

            user.Business.BusinessInvestments.Add(investorBusinessInvestment);
            companyToEspionage.BusinessInvestments.Add(investeeBusinessInvestment);
            _context.Business.Update(companyToEspionage);
            _context.Business.Update(user.Business);
            await _context.SaveChangesAsync();

            return(Ok(JsonConvert.SerializeObject(new { SuccessfulEspionage = true, EspionageAmount = espionageAmount, UpdatedEspionageCost = CalculateEspionageCost(user.Business) })));
        }
        public async Task <IActionResult> GroupInvestInCompany(int companyToPartnerWith, int businessSendingRequest, int companyToInvestInId, double investmentAmount)
        {
            var user = await GetCurrentEntrepreneur();

            var companyToInvestIn = await _context.Business.SingleOrDefaultAsync(s => s.Id == companyToInvestInId);

            var partnerCompany = await _context.Business.SingleOrDefaultAsync(s => s.Id == companyToPartnerWith);

            if (investmentAmount <= 0 || investmentAmount > user.Business.CashPerSecond || investmentAmount > partnerCompany.CashPerSecond)
            {
                return(RedirectToAction("Index", "Business", new { id = companyToInvestInId }));
            }

            var investment = new Investment()
            {
                InvestmentAmount                          = investmentAmount,
                InvestmentExpiration                      = DateTime.UtcNow.AddDays(1),
                InvestedBusinessCashAtInvestment          = companyToInvestIn.Cash,
                InvestedBusinessCashPerSecondAtInvestment = companyToInvestIn.CashPerSecond,
                InvestmentType = InvestmentType.Group,
            };
            var investorBusinessInvestment = new BusinessInvestment()
            {
                Business            = user.Business,
                Investment          = investment,
                InvestmentType      = InvestmentType.Group,
                InvestmentDirection = InvestmentDirection.Investor
            };
            var partnerBusinessInvestment = new BusinessInvestment()
            {
                Business            = partnerCompany,
                Investment          = investment,
                InvestmentType      = InvestmentType.Group,
                InvestmentDirection = InvestmentDirection.Partner
            };
            var companyToInvestInBusinessInvestment = new BusinessInvestment()
            {
                Business            = companyToInvestIn,
                Investment          = investment,
                InvestmentType      = InvestmentType.Group,
                InvestmentDirection = InvestmentDirection.Investee
            };

            _context.BusinessInvestments.Add(investorBusinessInvestment);
            _context.BusinessInvestments.Add(partnerBusinessInvestment);
            _context.BusinessInvestments.Add(companyToInvestInBusinessInvestment);
            await _context.SaveChangesAsync();

            var bsr = await _context.Business.Where(s => s.Id == businessSendingRequest).SingleOrDefaultAsync();

            var investmentLink = @$ "<a href='/Business/GroupInvestment/{investment.Id}'>Click here to accept</a>";
            var message        = $"{bsr.Name} wants to invest ${investmentAmount.ToKMB()} in {companyToInvestIn.Name} with you. {investmentLink}";

            partnerCompany.ReceivedMessages.Add(
                new Message()
            {
                DateReceived = DateTime.UtcNow, SendingBusinessId = businessSendingRequest, ReceivingBusinessId = companyToPartnerWith, MessageBody = message
            });
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index", "Business", new { id = companyToInvestInId }));
        }