using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using OneProjectTableAdapters;
using System.Data;
using System.Data.SqlClient;

[WebService(Namespace = "http://plexityhide.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{

    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

  private void InitializeComponent()
  {

  }
  [WebMethod]
  public Projects GetProjects()
  {
    Projects ds=new Projects();
    
    ProjectsTableAdapters.ProjectTableAdapter x=new ProjectsTableAdapters.ProjectTableAdapter();
    x.Connection.Open();
    x.Fill(ds.Project);
    x.Connection.Close();
    return ds;
  }

  [WebMethod]
  public int CreateNewProject(string aDescription)
  {
    Projects ds = new Projects();
    Projects.ProjectRow r=ds.Project.NewProjectRow();
    ds.Project.AddProjectRow(r);
    r.description = aDescription;
    ProjectsTableAdapters.ProjectTableAdapter x = new ProjectsTableAdapters.ProjectTableAdapter();
    x.Connection.Open();
    x.Update(ds.Project);
    // and fetch the new identity
    System.Data.SqlClient.SqlCommand q = x.Connection.CreateCommand();
    q.CommandText = "SELECT @@IDENTITY AS NewID";
    int newid = Convert.ToInt32(q.ExecuteScalar());
    x.Connection.Close();

    return newid;
  }

  [WebMethod]
  public Projects UpdateProjects(Projects ds)
  {
    ProjectsTableAdapters.ProjectTableAdapter x = new ProjectsTableAdapters.ProjectTableAdapter();
    x.Connection.Open();
    x.Update(ds.Project);
    x.Connection.Close();
    return GetProjects(); // reread to clear conflicts and fetch identities
  }

    
  [WebMethod]
  public OneProject GetOneProject(int aProjectId)
  {
    PlannedWorkTableAdapter x1=new PlannedWorkTableAdapter();
    WorkLinksTableAdapter x2=new WorkLinksTableAdapter();
    WorkNodeTableAdapter x3=new WorkNodeTableAdapter();

    OneProject xds=new OneProject();

    x1.Fill(xds.PlannedWork, aProjectId);
    x2.Fill(xds.WorkLinks, aProjectId);
    x3.Fill(xds.WorkNode, aProjectId);

    return xds;
  }
  
  public void CheckIDOnRow(DataRow r,SqlConnection c,ref string result)
  {
    result+=" CheckIDOnRow";
    if (r.RowState!=DataRowState.Deleted && r.RowState!=DataRowState.Detached &&  (int)r["id"]==0)
    {
      System.Data.SqlClient.SqlCommand q = c.CreateCommand();
      q.CommandText = "SELECT @@IDENTITY AS NewID";
      r["id"] = Convert.ToInt32(q.ExecuteScalar());
      r.AcceptChanges();    
    }
  
  }
  [WebMethod]
  public void AddDataToProject(int projid)
  {
    
    OneProject p=GetOneProject(projid);
    
    // Add Parent node
    OneProject.WorkNodeRow r = p.WorkNode.NewWorkNodeRow();
    r.parentNode=-1;
    r.description="Demo1";
    p.WorkNode.AddWorkNodeRow(r);
    string result;
    DataSet ds=UpdateOneProject(projid,p.GetChanges(),out result);
    p.Merge(ds, false);
    p.AcceptChanges();


    int nodeid = (int)ds.Tables["workNode"].Rows[0]["id"];

    // Add Sub Node
    r = p.WorkNode.NewWorkNodeRow();
    r.parentNode = nodeid;
    r.description = "Demo2";
    p.WorkNode.AddWorkNodeRow(r);
    ds = UpdateOneProject(projid, p.GetChanges(), out result);
    p.Merge(ds, false);
    p.AcceptChanges();
    
    int subnodeid=(int)ds.Tables["workNode"].Rows[0]["id"];

    // Add Time item on both nodes
    OneProject.PlannedWorkRow rw = p.PlannedWork.NewPlannedWorkRow();
    rw.node=nodeid;
    rw.start=DateTime.Now;
    rw.stop=rw.start.AddDays(1);
    p.PlannedWork.AddPlannedWorkRow(rw);

    rw = p.PlannedWork.NewPlannedWorkRow();
    rw.node = subnodeid;
    rw.start = DateTime.Now.AddDays(1);
    rw.stop = rw.start.AddDays(1);
    p.PlannedWork.AddPlannedWorkRow(rw);
    
    ds = UpdateOneProject(projid, p.GetChanges(), out result);
    p.Merge(ds, false);
    p.AcceptChanges();

    int timeitem1id = (int)ds.Tables["plannedWork"].Rows[0]["id"];
    int timeitem2id = (int)ds.Tables["plannedWork"].Rows[1]["id"];
    
    // Add a link
     OneProject.WorkLinksRow wlr=p.WorkLinks.NewWorkLinksRow();
     wlr.start=timeitem1id;
     wlr.target=timeitem2id;

     p.WorkLinks.AddWorkLinksRow(wlr);
     ds = UpdateOneProject(projid, p.GetChanges(), out result);
     p.Merge(ds, false);
     p.AcceptChanges();
     
    
    // Try changing the description
    p.WorkNode.Rows[0]["description"]=DateTime.Now.ToString();
    ds = UpdateOneProject(projid, p.GetChanges(), out result);
    p.Merge(ds, false);
    p.AcceptChanges();
    
    
    
    
  }  
  [WebMethod]
  public DataSet UpdateOneProject(int projid, DataSet ds, out string result)
  {
    result="1";
    PlannedWorkTableAdapter x1 = new PlannedWorkTableAdapter();
    WorkLinksTableAdapter x2 = new WorkLinksTableAdapter();
    WorkNodeTableAdapter x3 = new WorkNodeTableAdapter();
    result = "2";

    try
    {
      
      x1.Connection.Open();
      x2.Connection=x1.Connection;
      x3.Connection = x1.Connection;
      try
      {
        result = "2.1";
        DataRow r;
        for (int i = 0; i < ds.Tables["PlannedWork"].Rows.Count; i++)
        {
          r=ds.Tables["PlannedWork"].Rows[i];
          if (r.RowState != DataRowState.Deleted && r["project"].ToString() != projid.ToString())
            r["project"] = projid;
          x1.Update(r);
          CheckIDOnRow(r, x1.Connection, ref result);
        }
        result = "2.2";

        for (int i = 0; i < ds.Tables["WorkLinks"].Rows.Count; i++)
        {
          r=ds.Tables["WorkLinks"].Rows[i];
          if (r.RowState != DataRowState.Deleted && r["project"].ToString() != projid.ToString())
            r["project"] = projid;
          x2.Update(r);
          CheckIDOnRow(r, x1.Connection, ref result);
        }
        result = "2.3";
        for (int i = 0; i < ds.Tables["WorkNode"].Rows.Count; i++)
        {
          result = "2.3.1";
          r = ds.Tables["WorkNode"].Rows[i];
          if (r.RowState!=DataRowState.Deleted && r["project"].ToString() != projid.ToString())
            r["project"] = projid;
          result = "2.3.2";
          x3.Update(r);
          result = "2.3.3";
          CheckIDOnRow(r, x1.Connection, ref result);
        }

        result = "";
        
        
      }
      catch(Exception e)
      {
        result += e.Message;
      }
    }
    catch (Exception e2)
    {
      result+=e2.Message;
    }
  
    

    return ds;
  }
    
}
