using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.Objects;
using Eyepax.Test.Model;
using System.Data.Entity.Infrastructure;
using System.Configuration;

namespace Eyepax.Test.Data
{

public class TestDAL
{
public List<Test> GetTestAll()
{
using (TestEntities _db = new TestEntities(ConfigurationManager.ConnectionStrings[0].ToString()))
{
return _db.Tests.ToList();
}
}

public List<Eyepax.Test.Model.Test> GetTestByIDSP(int? id)
{
using (TestEntities _db = new TestEntities())
{
var p_studentID = id.HasValue ?
new ObjectParameter("studentID", id) :
new ObjectParameter("studentID", typeof(int));

List<Eyepax.Test.Model.Test> empDetails = _db.Database.SqlQuery<Eyepax.Test.Model.Test>("exec spTest @studentID", p_studentID).ToList();
return empDetails;
}
}

public Test GetTest(int? id)
{
using (TestEntities _db = new TestEntities(ConfigurationManager.ConnectionStrings[0].ToString()))
{
Test emp = _db.Tests.FirstOrDefault(x => x.ID == id);
return emp;
}
}

/// <summary>
/// Get All Student By Stored Procedure
/// </summary>
/// <param name="id"></param>
/// <returns>Test Object List</returns>
public List<Eyepax.Test.Model.Test> GetTestByIDLinq(int? id)
{
/*
using (TestEntities _db = new TestEntities(ConfigurationManager.ConnectionStrings[0].ToString()))
{
var result = from student in _db.Tests
// where student.ID == id
join department in _db.TestRows
on student.DepID equals department.DepID
select student;

List<Eyepax.Test.Model.Test> temp = result.ToList().Select(obj => new Eyepax.Test.Model.Test
{
ID = obj.ID,
Name = obj.Name,
Address = obj.Address,
DepID = obj.DepID,
Donation = obj.Donation,
DepartmentName = obj.DepartmentName
}).ToList();
return temp;
}
*/

List<Eyepax.Test.Model.Test> temp = new List<Model.Test>();
temp.Add(new Model.Test() { ID = 1, DepartmentName = "gh", DepID = 1, Donation = 326.0M, Address = "sda", Name = "GHGJHJ" });
temp.Add(new Model.Test() { ID = 2, DepartmentName = "gh", DepID = 1, Donation = 3726.0M, Address = "sda", Name = "IIHHS" });
temp.Add(new Model.Test() { ID = 3, DepartmentName = "gh", DepID = 1, Donation = 326457.0M, Address = "sda", Name = "sfdsf" });
temp.Add(new Model.Test() { ID = 4, DepartmentName = "gh", DepID = 1, Donation = 3226.0M, Address = "sda", Name = "IIHerweHS" });

return temp;


}

public decimal GetDonationDetails(int? id)
{
using (TestEntities _db = new TestEntities(ConfigurationManager.ConnectionStrings[0].ToString()))
{
return _db.Tests.Where(x => x.ID == id).Select(x => x.Donation).Single();
}
}

public void CreateTest(Eyepax.Test.Model.Test tst)
{
using (TestEntities _db = new TestEntities(ConfigurationManager.ConnectionStrings[0].ToString()))
{
_db.Tests.Add(new Test() { ID= tst.ID,Name= tst.Name, Address = tst.Address, Donation = tst.Donation, DepID = tst.DepID, DepartmentName = tst.DepartmentName});
_db.SaveChanges();
}
}

public void Update(Eyepax.Test.Model.Test tstObj)
{
using (TestEntities _db = new TestEntities(ConfigurationManager.ConnectionStrings[0].ToString()))
{
//TestServiceReference.Test tempTest = (TestServiceReference.Test)_db.Tests.SingleOrDefault(x => x.ID == tstObj.ID);
//tempTest.Name = tstObj.Name;
// tempTest.DepID = tstObj.DepartmentName;

_db.SaveChanges();
}
}

Public decimal GetSal(int id)

{

_db = new EmployeeTableEntities();

return _db.EmployeeTables.Where (x => x.ID == id).single().BasicSalary;

}

}
}