using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace DemoExam.Model
{
    partial class Agent:INotifyPropertyChanged
    {
        public int GetCountSaleOnYear
        {
            get
            {
                int countSale = 0;
                var listSale = DataBase.Base.ProductSale.Where(x => x.AgentID == ID);
                if (listSale != null && listSale.Count() != 0)
                {
                    foreach (var item in listSale)
                    {
                        if ((DateTime.Now - (DateTime)item.SaleDate).Days < 365)
                        {
                            countSale += item.ProductCount;
                        }
                    }
                }
                return countSale;
            }
        }

        public string GetActualImage
        {
            get
            {
                string pathImage = Environment.CurrentDirectory + Logo;
                if (Logo == null)
                {
                    pathImage = Environment.CurrentDirectory + @"\images\picture.png";
                }
                return pathImage;
            }
        }

        public double GetDiscont
        {
            get
            {
                double discount = 0;
                double sum = 0;
                var listSale = DataBase.Base.ProductSale.Where(x => x.AgentID == ID);
                foreach (var item in listSale)
                {
                    sum += Convert.ToDouble(item.ProductCount * item.Product.MinCostForAgent);
                }

                if (sum >= 0 && sum < 10000)
                {
                    discount = 0;
                }
                if (sum >= 10000 && sum < 50000)
                {
                    discount = 0.05;
                }
                if (sum > 50000 && sum < 150000)
                {
                    discount = 0.1;
                }
                if (sum > 150000 && sum < 500000)
                {
                    discount = 0.2;
                }
                if (sum > 500000)
                {
                    discount = 0.25;
                }
                return discount;

            }
        }

        public string GetDiscountToString
        {
            get
            {
                return (GetDiscont * 100).ToString() + "%";
            }
        }


        public bool ChangeBackground
        {
            get
            {
                if (GetDiscont != 0.25) return false;
                return true;
            }

        }





        public RoutedCommand ChangeImageCommand { get; } = new RoutedCommand();
        public CommandBinding ChangeImageBinding;

        public event PropertyChangedEventHandler PropertyChanged;

        public void ChangeImageFunc(object sender, EventArgs e)
        {
            string fileName = string.Empty;
            OpenFileDialog fileDialog = new OpenFileDialog();
            if (fileDialog.ShowDialog() == true)
            {
                fileName = fileDialog.SafeFileName;
                File.Copy(fileDialog.FileName, Environment.CurrentDirectory + @"\agents\" + fileName, true);

            }
            Logo = @"\agents\" + fileName;
            PropertyChanged(this, new PropertyChangedEventArgs("GetActualImage"));
        }



    }
}
