using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace migrate_data
{
    public class GDSpecObj
    {
        private string gDspecInsert = "insert into z_gdspec(ID, [RPM Part Number], ERN, Application, Notes, Signed, [Date Signed], Checked, [Date Checked], [Effective Date], Obsolete, [Reason for change], [Work Instruction], [Customer Notification], [FAC Required], [FAC Completed], Validation, Replaces) values({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17})";
        public string ReturnQuery()
        {
            string signedD, chkD, effectiveD;
            if (this.datesigned == null) signedD = string.Format("null");
            else signedD = string.Format("'{0}'", this.datesigned);
            if (this.chkdate == null) chkD = string.Format("null");
            else chkD = string.Format("'{0}'", this.chkdate);
            if (this.effectivedate == null) effectiveD = string.Format("null");
            else effectiveD = string.Format("'{0}'", effectivedate);
            return string.Format(gDspecInsert
                , this.id
                , this.number
                , this.ern
                , this.app
                , this.notes
                , this.signed
                , signedD
                , this.chkd
                , chkD
                , effectiveD
                , this.obsolete
                , this.rsfc
                , this.wrkinst
                , this.custnot
                , this.facreq
                , this.faccomp
                , this.valid
                , this.replaces);
        }
        
        public int id { get; set; }
        public string number{get;set;}
        public string ern { get; set; }
        public string app { get; set; }
        public string notes { get; set; }
        public string signed { get; set; }
        public DateTime? datesigned { get; set; }
        public string chkd { get; set; }
        public DateTime? chkdate { get; set; }
        public DateTime? effectivedate { get; set; }
        public int obsolete { get; set; }
        public string rsfc { get; set; }
        public string wrkinst { get; set; }
        public int custnot { get; set; }
        public int facreq { get; set; }
        public int faccomp { get; set; }
        public string valid { get; set; }
        public string replaces { get; set; }

        public string SingleQuotes(string str)
        {
            if (string.IsNullOrEmpty(str))
                return "null";
            else return string.Format("'{0}'", str);
        }

        public int ConvertBit(int i)
        {
            if (i < 0) return 1;
            else return 0;
        }

        public DateTime? DateVal(string dt)
        {
            bool isValid = false;
            DateTime date;
            isValid = DateTime.TryParse(dt, out date);
            if (isValid) return date;
            else return null;
        }

        public void SetVals(DataRow row)
        {
            this.id = Convert.ToInt32(row.ItemArray[0]);
            this.number = SingleQuotes(row.ItemArray[1].ToString());
            this.ern = SingleQuotes(row.ItemArray[2].ToString().Replace("'", ""));
            this.app = SingleQuotes(row.ItemArray[4].ToString().Replace("'", ""));
            this.replaces = SingleQuotes(row.ItemArray[3].ToString().Replace("'", ""));
            this.notes = SingleQuotes(row.ItemArray[5].ToString().Replace("'", ""));
            this.signed = SingleQuotes(row.ItemArray[6].ToString().Replace("'", ""));
            this.datesigned = DateVal(row.ItemArray[7].ToString());
            this.chkd = SingleQuotes(row.ItemArray[8].ToString().Replace("'", ""));
            this.chkdate = DateVal(row.ItemArray[9].ToString());
            this.effectivedate = DateVal(row.ItemArray[10].ToString());
            this.obsolete = ConvertBit(Convert.ToInt32(row.ItemArray[11]));
            this.rsfc = SingleQuotes(row.ItemArray[12].ToString().Replace("'", ""));
            this.wrkinst = SingleQuotes(row.ItemArray[13].ToString().Replace("'", ""));
            this.custnot = ConvertBit(Convert.ToInt32(row.ItemArray[14]));
            this.facreq = ConvertBit(Convert.ToInt32(row.ItemArray[15]));
            this.faccomp = ConvertBit(Convert.ToInt32(row.ItemArray[16]));
            this.valid = SingleQuotes(row.ItemArray[17].ToString().Replace("'", ""));
        }
    }
}
