using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;



namespace tespASP2
{
    public class Row
    {
        public int rowId;
        public string rowTitle;
        public Dictionary<string, Stripe> stripes;

        public Row(int id, string title)
        {
            rowId = id;
            rowTitle = title;
            stripes = new Dictionary<string, Stripe>( );
        }
    }

    public class Stripe
    {
        public int stripeId;
        public string stripeTitle;
        public Int64 startTs;
        public Int64 endTs;
        public string color;
        public int rowId;
        public bool visible;

        public Stripe ()
        {

        }
        public Stripe ( int id, string title, Int64 start, Int64 end, string clr, int row )
        {
            stripeId=id;
            stripeTitle = title;
            startTs = start;
            endTs = end;
            color = clr;
            rowId = row;
            visible = true;
        }
        public Stripe( int id, string title, Int64 start, Int64 end, string clr, int row, bool vsbl )
        {
            stripeId = id;
            stripeTitle = title;
            startTs = start;
            endTs = end;
            color = clr;
            rowId = row;
            visible = vsbl;
        }
    }

    /// <summary>
    /// Summary description for GanttData
    /// </summary>
    [WebService( Namespace = "http://tempuri.org/" )]
    [WebServiceBinding( ConformsTo = WsiProfiles.BasicProfile1_1 )]
    [System.ComponentModel.ToolboxItem( false )]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class GanttData : System.Web.Services.WebService
    {
        System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer( );
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

 [WebMethod]    
    public string LoadGanttData(string startDate, string endDate)
    {
      DateTime? startTs = null;
      DateTime? endTs = null;
      
      Int64 DatetimeMinTimeTicks = (new DateTime( 1970, 1, 1, 0, 0, 0, DateTimeKind.Utc )).Ticks;
      Int64 ticks;
            
      if (Int64.TryParse( startDate, out ticks ))
      {
          startTs = new DateTime( (ticks*10000 + DatetimeMinTimeTicks) );
      }

      if (Int64.TryParse( endDate, out ticks ))
      {
          endTs = new DateTime( (ticks*10000 + DatetimeMinTimeTicks) );
      }
      
      DataTable dt = SqlProc.GanttLoadShiftData.ExecuteTable(startTs, endTs);
      
      Dictionary<string, object> response = new Dictionary<string, object>( );
      Dictionary<string, Row> rows = new Dictionary<string, Row>( );
            
      foreach(DataRow tr in dt.Rows)
      {
        int rowId = System.Convert.ToInt32( tr["SHF_ProdShift.DMT_DeviceStructId.p_id"].ToString( ).Trim( ) );
        string rowTitle = tr["SHF_ProdShift.SHF_ShiftId.Code"].ToString( ).Trim( );

        int stripeId = System.Convert.ToInt32( tr["SHF_ProdShift.p_id"].ToString( ).Trim( ) );
        string stripeTitle = tr["SHF_ProdShift.SHF_ShiftId.Code"].ToString( ).Trim( );
        
        string color = tr["SHF_ProdShift.SHF_ShiftId.ColorBg"].ToString( ).Trim( );

        Int64 rowStartTs = (System.Convert.ToDateTime( tr["SHF_ProdShift.BeginTime"] ).Ticks - DatetimeMinTimeTicks)/10000;
        Int64 rowEndTs = (System.Convert.ToDateTime( tr["SHF_ProdShift.EndTime"] ).Ticks - DatetimeMinTimeTicks) / 10000;

        Stripe stripe = new Stripe( stripeId, stripeTitle, rowStartTs, rowEndTs, color, rowId);
        
        if (rows.ContainsKey(rowId.ToString() ))
        {
            rows[rowId.ToString() ].stripes.Add( stripe.stripeId.ToString(), stripe );
        }
        else
        {
            Row row = new Row( rowId, rowTitle );
            row.stripes.Add( stripe.stripeId.ToString(), stripe );
            rows.Add( row.rowId.ToString(), row );
        }
      }
      response.Add( "response",  "Succes");
      response.Add( "message", "Ok" );
      response.Add( "data", rows );
            
      return js.Serialize( response );     
    } 
    
    [WebMethod]
    public string SaveGanttData(Stripe stripe)
    {
        DateTime? startTs = null;
        DateTime? endTs = null;
        Int32? rowId = null;
        Int32? stripeId = null;

        Int64 DatetimeMinTimeTicks = (new DateTime( 1970, 1, 1, 0, 0, 0, DateTimeKind.Utc )).Ticks;
        Int64 ticks;
        int number;

        if (Int64.TryParse( stripe.startTs.ToString(), out ticks ))
        {
            startTs = new DateTime( (ticks * 10000 + DatetimeMinTimeTicks) );
        }

        if (Int64.TryParse( stripe.endTs.ToString(), out ticks ))
        {
            endTs = new DateTime( (ticks * 10000 + DatetimeMinTimeTicks) );
        }

        if (Int32.TryParse( stripe.rowId.ToString(), out number ))
        {
            rowId = number;
        }

        if (Int32.TryParse( stripe.stripeId.ToString(), out number ))
        {
            stripeId = number;
        }

        startTs.Value.AddHours( 1 );
        endTs.Value.AddHours( 1 );

        int updated = (int)SqlProc.GanttSaveShiftData.ExecuteValue(stripeId, startTs, endTs, rowId);
        
        Dictionary<string, object> response = new Dictionary<string, object>( );
        
        if(updated==1)
        {
          response.Add( "response",  "Succes");
          response.Add( "message", "Ok" );        
        }
            
        return js.Serialize( response );     
    }  

    }
}
