using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public class Cart {
    private String _isbn;
    private String _title;
    private int _price;
    private int _quantity;
    private int _subTotal;
    public String Isbn {
        get {
            return this._isbn;
        }
        set {
            this._isbn = value;
        }
    }
    public String Title {
        get {
            return this._title;
        }
        set {
            this._title = value;
        }
    }
    public int Price {
        get {
            return this._price;
        }
        set {
            this._price = value;
        }
    }
    public int Quantity {
        get {
            return this._quantity;
        }
        set {
            this._quantity = value;
        }
    }
    public int SubTotal {
        get {
            return this._subTotal;
        }
        set {
            this._subTotal = value;
        }
    }
    public ArrayList GetCartInfo() {
        ArrayList list = new ArrayList();
        HttpContext Context  = HttpContext.Current;
        HttpSessionState Session = Context.Session;
        for(int i=0;i<Session.Count;i++){
            Cart crt = new Cart();
            crt.Isbn = Session.Keys[i];
            crt.Quantity = (int)(Session[i]);
            ConnectionStringSettings setting = 
              ConfigurationManager.ConnectionStrings["MyDB"];
            DbProviderFactory factory = 
              DbProviderFactories.GetFactory(setting.ProviderName);
            using(DbConnection db= factory.CreateConnection()){
                db.ConnectionString = setting.ConnectionString;
                DbCommand comm = factory.CreateCommand();
                comm.CommandText = 
                  "SELECT title, price FROM Book WHERE isbn=@isbn";
                comm.Connection = db;
                DbParameter param = factory.CreateParameter();
                param.ParameterName = "@isbn";
                param.Value = crt.Isbn;
                comm.Parameters.Add(param);
                db.Open();
                DbDataReader reader = comm.ExecuteReader();
                if(reader.Read()) {
                    crt.Title = reader.GetString(0);
                    crt.Price = reader.GetInt32(1);
                    crt.SubTotal = crt.Price * crt.Quantity;
                    list.Add(crt);
                }
            }
        }
        return list;
    }

    public void DeleteCartInfo(Cart crt) {
        HttpContext Context  = HttpContext.Current;
        HttpSessionState Session = Context.Session;
        Session.Remove(crt.Isbn);
    }
    public void InsertCartInfo() {
        HttpContext Context = HttpContext.Current;
        HttpSessionState Session = Context.Session;
        ConnectionStringSettings setting = 
          ConfigurationManager.ConnectionStrings["MyDB"];
        DbProviderFactory factory = 
          DbProviderFactories.GetFactory(setting.ProviderName);
        using(DbConnection db = factory.CreateConnection()) {
            db.ConnectionString = setting.ConnectionString;
            DbCommand comm = factory.CreateCommand();
            comm.CommandText = 
              "INSERT INTO OrderInfo(uid,isbn,quantity) VALUES(@uid, @isbn, @quantity)";
            comm.Connection = db;
            db.Open();
            for(int i=0;i<Session.Count;i++){
                comm.Parameters.Clear();
                DbParameter pUid = factory.CreateParameter();
                pUid.ParameterName = "@uid";
                pUid.Value = Context.User.Identity.Name;
                comm.Parameters.Add(pUid);
                DbParameter pIsbn = factory.CreateParameter();
                pIsbn.ParameterName = "@isbn";
                pIsbn.Value = Session.Keys[i];
                comm.Parameters.Add(pIsbn);
                DbParameter pQuantity = factory.CreateParameter();
                pQuantity.ParameterName = "@quantity";
                pQuantity.Value = Session[i];
                comm.Parameters.Add(pQuantity);
                comm.ExecuteNonQuery();
            }
        }
    }
}