using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using edu.jhu.Cas.Util;
using System.Data;

namespace RestApi.Models
{
    public class JobInfo
    {
        public long JobID { get; set; }

        public long? Rows { get; set; }

        public long WebServicesID { get; set; }

        public DateTime? TimeSubmit { get; set; }

        public DateTime? TimeStart { get; set; }

        public DateTime? TimeEnd { get; set; }

        public int? SendEmail { get; set; }

        public int Status { get; set; }

        public int AutoComplete { get; set; }

        public int Estimate { get; set; }

        public string TaskName { get; set; }

        public string OutputLoc { get; set; }

        public string HostIP { get; set; }

        public string Message { get; set; }

        public string Query { get; set; }

        public string ModifiedQuery { get; set; }

        public string Target { get; set; }

        public string OutputType { get; set; }

        public string Params { get; set; }

        public string Created_Table { get; set; }
        

        private JobInfo() { }

        public static JobInfo FromId(long id, long wsid)
        {
            JobInfo result = null;
            
            string query =
                "SELECT * FROM Jobs WHERE JobID = " + id + " AND WebServicesID = " + wsid;
            
            DataTable table = SqlFactory.select(query).Tables[0];

            if (table.Rows.Count > 0)
            {
                result = new JobInfo();
                result.AutoComplete = (int)table.Rows[0]["AutoComplete"];
                result.Created_Table = table.Rows[0]["Created_Table"] as string;
                result.Estimate = (int)table.Rows[0]["Estimate"];
                result.HostIP = table.Rows[0]["HostIP"] as string;
                result.JobID = (long)table.Rows[0]["JobID"];
                result.Message = table.Rows[0]["Message"] as string;
                result.ModifiedQuery = table.Rows[0]["ModifiedQuery"] as string;
                result.OutputLoc = table.Rows[0]["OutputLoc"] as string;
                result.OutputType = table.Rows[0]["OutputType"] as string;
                result.Params = table.Rows[0]["Params"] as string;
                result.Query = table.Rows[0]["Query"] as string;
                result.Rows = table.Rows[0]["Rows"] as long?;
                result.SendEmail = table.Rows[0]["SendEmail"] as int?;
                result.Status = (int)table.Rows[0]["Status"];
                result.Target = table.Rows[0]["Target"] as string;
                result.TaskName = table.Rows[0]["TaskName"] as string;
                result.TimeEnd = table.Rows[0]["TimeEnd"] as DateTime?;
                result.TimeStart = table.Rows[0]["TimeStart"] as DateTime?;
                result.TimeSubmit = table.Rows[0]["TimeSubmit"] as DateTime?;
                result.WebServicesID = (long)table.Rows[0]["WebServicesID"];
            }

            return result;
        }
    }
}