using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demoexample
{
    public class ViewModel
    {
        public List<Agent> agents;
        public ViewModel()
        {
            agents = newagent();
        }

        public List<Agent> newagent()
        {
            List<Agent> allagent = new List<Agent>();
            Agent buffer;
            List<Agent> bdagents = DataBase.BaseModel.Agent.ToList();
            List<Product> bdproducts = DataBase.BaseModel.Product.ToList();
            List<ProductSale> bdproductSales = DataBase.BaseModel.ProductSale.ToList();

            foreach(Agent agent in bdagents)
            {
                buffer = new Agent();


                buffer.ID = agent.ID;
                buffer.Title = agent.Title;
                buffer.Phone = agent.Phone;
                buffer.Priority = agent.Priority;
                buffer.AgentTypeID = agent.AgentTypeID;

                AgentType AT = DataBase.BaseModel.AgentType.FirstOrDefault(x => x.ID == agent.AgentTypeID);
                buffer.TypeOfAgent = AT.Title;

                int productCount = 0;
                int allProductCount = 0;

                foreach(ProductSale productSale in bdproductSales)
                {
                    if(agent.ID == productSale.AgentID)
                    {
                        if(productSale.SaleDate.Year > 2021)
                        {
                            productCount += productSale.ProductCount;
                        }
                        allProductCount += productSale.ProductCount;
                    }
                }

                buffer.Count = productCount;
                if(allProductCount>0 && allProductCount<10000)
                {
                    buffer.Sale = 0;
                }
                else if(allProductCount > 10000 && allProductCount < 50000)
                {
                    buffer.Sale = 5;
                }
                else if (allProductCount > 50000 && allProductCount < 150000)
                {
                    buffer.Sale = 10;
                }
                else if (allProductCount > 150000 && allProductCount < 500000)
                {
                    buffer.Sale = 20;
                }
                else if (allProductCount > 500000 && allProductCount < 1500000)
                {
                    buffer.Sale = 25;
                }
                else if (allProductCount > 1500000 && allProductCount < 5000000)
                {
                    buffer.Sale = 30;
                }
                
                if(buffer.Sale > 25)
                {
                    buffer.Color = "#FF99EA81";
                }

                buffer.Logo = agent.Logo;
                if(agent.Logo == "")
                {
                    buffer.Logo = "/img/picture.png";
                }

                allagent.Add(buffer);
            }
            return allagent;
        }
    }
}
