using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DAL;

namespace BLL
{
    public class Customer
    {
        string _cid;
        string _fname;
        string _lname;
        string _tel;
        string _address;
        string _bedehkar;
        string _talabkar;

        public Customer()
        {

        }

        public string cid
        {
            get
            {
                return _cid;
            }
            set
            {
                this._cid = value;
                
            }
        }
        public string address
        {
            get
            {
                return _address;
            }
            set
            {
                this._address = value;
                
            }
        }
        public string bedehkar
        {
            get
            {
                return _bedehkar;
            }
            set
            {
                this._bedehkar = value;
                
            }
        }
        public string fname
        {
            get
            {
                return _fname;
            }
            set
            {
                this._fname = value;
                
            }
        }
        public string lname
        {
            get
            {
                return _lname;
            }
            set
            {
                this._lname = value;
                
            }
        }
        public string talabkar
        {
            get
            {
                return _talabkar;
            }
            set
            {
                this._talabkar = value;
                
            }
        }
        public string tel
        {
            get
            {
                return _tel;
            }
            set
            {
                this._tel = value;
                
            }
        }

        public void Insert()
        {
            CustomerMethods methods = new CustomerMethods();
            methods.Insert(this.cid, this.fname, this.lname, this.tel, this.address, this.bedehkar, this.talabkar);
        }

        public void Update()
        {
            CustomerMethods methods = new CustomerMethods();
            methods.Update(this.cid, this.fname, this.lname, this.tel, this.address, this.bedehkar, this.talabkar);
        }

        public void Delete()
        {
            CustomerMethods methods = new CustomerMethods();
            methods.Delete(this.cid);
        }

        public void AddToBedehkari(string mablagh)
        {
            CustomerMethods methods = new CustomerMethods();
            methods.AddToBedehkari(this.cid, mablagh);
        }
        public static List<Customer> GetAllCustomers()
        {
            CustomerMethods methods = new CustomerMethods();
            return GetAllCustomers((IDataReader)methods.SelectAll());
        }

        public static List<Customer> GetCustomer(long cid)
        {
            CustomerMethods methods = new CustomerMethods();
            return GetAllCustomers((IDataReader)methods.Select(cid));
        }

        private static List<Customer> GetAllCustomers(IDataReader reader)
        {
            List<Customer> tempList = new List<Customer>();
            while (reader.Read())
            {
                try
                {
                    Customer temp = new Customer
                    {
                        cid = reader["cid"].ToString(),
                        fname = reader["fname"].ToString(),
                        lname = reader["lname"].ToString(),
                        tel = reader["tel"].ToString(),
                        address = reader["address"].ToString(),
                        bedehkar = reader["bedehkar"].ToString(),
                        talabkar = reader["talabkar"].ToString()
                    };
                    tempList.Add(temp);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message.ToString());
                }
            }
            return tempList;
        }
    }
}
