using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyTunes.Common.DAL;
using MyTunes.Common.DAL.MyTunesDataSetTableAdapters;

namespace MyTunes.Common.BLL
{
    [System.ComponentModel.DataObject]
    public class CustomerBLL
    {
        private CustomerTableAdapter _customerAdapter;

        protected CustomerTableAdapter Adapter
        {
            get
            {
                if (_customerAdapter == null)
                    _customerAdapter = new CustomerTableAdapter();
                return _customerAdapter;
            }
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public MyTunesDataSet.CustomerDataTable GetCustomers()
        {
            return Adapter.GetCustomers();
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public bool AddCustomer(string firstName, string lastName, string email, string password, bool isAdmin)
        {
            MyTunesDataSet.CustomerDataTable customers = new MyTunesDataSet.CustomerDataTable();

            MyTunesDataSet.CustomerRow customer = customers.NewCustomerRow();
            customer.first_name = firstName;
            customer.last_name = lastName;
            customer.email = email;
            customer.password = password;   // TODO: Encrypt this!
            customer.is_admin = isAdmin;

            customers.AddCustomerRow(customer);

            int rowsAffected = Adapter.Update(customers);
            return (rowsAffected == 1);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public bool UpdateCustomer(int customerID, string firstName, string lastName, string email, string password, string secret, DateTime expires, bool isAdmin)
        {
            MyTunesDataSet.CustomerDataTable customers = Adapter.GetCustomersByCustomerID(customerID);
            if (customers.Count == 0)
                return false;

            MyTunesDataSet.CustomerRow customer = customers[0];

            customer.first_name = firstName;
            customer.last_name = lastName;
            customer.email = email;
            customer.password = password;   // TODO: Encrypt this!
            customer.secret = secret;
            customer.secret_expires = expires;
            customer.is_admin = isAdmin;

            int rowsAffected = Adapter.Update(customer);
            return (rowsAffected == 1);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public bool DeleteCustomer(int customerID, byte[] changed)
        {
            int rowsAffected = Adapter.Delete(customerID, changed);
            return (rowsAffected == 1);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public bool DeleteCustomer(int customerID)
        {
            MyTunesDataSet.CustomerDataTable customers = Adapter.GetCustomersByCustomerID(customerID);
            if (customers.Count == 0)
                return false;
            return DeleteCustomer(customerID, customers[0].changed);
        }
    }
}