using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Collections;
using Telerik.OpenAccess;
using Telerik.OpenAccess.Query;
using DataProvider;

public partial class ORMGrid : System.Web.UI.Page
{
    Dictionary<object, object> values = new Dictionary<object, object>();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            //when you are using two-way binding the previous suggest approach is redundant
        }
    }
    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {
        string categoryID = ((GridEditFormItem)e.Item).ParentItem.GetDataKeyValue("CategoryID").ToString();
        ((GridEditFormItem)e.Item).ExtractValues(values); // just to verify that the correct data is extracted

        RadUpload upload = (e.Item as GridEditableItem)["Picture"].FindControl("RadUpload1") as RadUpload;
        string[] fileName = upload.UploadedFiles[0].FileName.Split('\\');
        values["ImageName"] = fileName[fileName.Length - 1];

        //must refactor
        using (IObjectScope objectScope = ObjectScopeProvider1.Database().GetObjectScope())
        {
            
            objectScope.Transaction.Begin();

            var category = GetCategory(objectScope, Convert.ToInt32(categoryID));

            if (category != null) 
            {
                try
                {
                    PopulateCategory(category, objectScope, values);
                    objectScope.Transaction.Commit();
                }
                catch (Exception ex) 
                {
                    //TODO: handle the exception
                }
            }
        }
        RadGrid1.EditIndexes.Clear();
        RadGrid1.Rebind();
    }

    private void PopulateCategory(Category category, IObjectScope objectScope, IDictionary values)
    {
        var categoryNameId = Convert.ToInt32(values["CategoryName.CategoryNameId"]);

        CategoryName categoryName = RetriveCategoryNameById(categoryNameId, objectScope);

        if (categoryName != null)
        {
            category.CategoryName = categoryName;
        }
        category.Description = values["Description"].ToString();
        category.Picture = values["ImageName"].ToString();
    }

    private CategoryName RetriveCategoryNameById(int categoryNameId, IObjectScope objectScope)
    {
        var contacts = objectScope.Extent<CategoryName>();
        return (from c in contacts
                where c.CategoryNameId == categoryNameId
                select c).FirstOrDefault();
    }

    private Category GetCategory(IObjectScope objectScope, int categoryID)
    {
        var orderHeaders = objectScope.Extent<Category>();
        return (from orderHeader in orderHeaders
                where orderHeader.CategoryID == categoryID
                select orderHeader).SingleOrDefault();
    }
}
