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

namespace ShabaninDemoEkz
{
    public class VModel
    {
        public List<Agent> agent;
        public List<AgentType> ta;
        public List<ProductSale> ps;
        public VModel()
        {
            agent = LoadDate();
        }

        public List<Agent> LoadDate()
        {
            agent = BaseConnect.BaseModel.Agent.ToList();
            ta = BaseConnect.BaseModel.AgentType.ToList();
            ps = BaseConnect.BaseModel.ProductSale.ToList();


            foreach (Agent a in agent)
            {
                if (a.Logo == "" || a.Logo == "null")
                {
                    a.Logo = "/Resurses/picture.png";
                }

                foreach (AgentType at in ta)
                {
                    if (a.AgentTypeID == at.ID)
                    {
                        a.typea = at.Title;
                    }
                }

                DateTime second = DateTime.Now;
                DateTime first = second.AddDays(-365);
                int count = 0;
                int scount = 0;
                foreach (ProductSale p in ps)
                {
                    if ((p.SaleDate > first && p.SaleDate < second) && a.ID == p.AgentID)
                    {
                        count += p.ProductCount;
                    }

                    if (a.ID == p.AgentID)
                    {
                        scount += p.ProductCount;
                    }

                }
                if (scount < 10000)
                {
                    a.skidka = 0;
                }
                if (scount >= 10000 && scount < 50000)
                {
                    a.skidka = 5;
                }
                if (scount >= 50000 && scount < 150000)
                {
                    a.skidka = 10;
                }
                if (scount >= 150000 && scount < 500000)
                {
                    a.skidka = 20;
                }
                if (scount >= 500000)
                {
                    a.skidka = 25;
                    a.grencolor = "LightGreen";
                }
                a.counts = count;



            }


            return agent;
        }
    }
}
