using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using com.clouddb.api;

/// <summary>
/// cloudDB Message Board Application
/// </summary>
[WebService(Namespace = "http://OpenLightGroup.net/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
    static string CloudDBUsername = "your cloudDB.com username (email)";
    static string CloudDBPassword = "your cloudDB.com password";

    private static Token LoginAndGetToken()
    {
        MessageBoard_1 CloudApp = new MessageBoard_1();
        TokenResponse objTokenResponse = 
            CloudApp.LogIn(CloudDBUsername, CloudDBPassword);
        return objTokenResponse.Result;
    }

    private static void Logout(Token LogoutToken)
    {
        MessageBoard_1 CloudApp = new MessageBoard_1();
        Outcome objOutcome = CloudApp.LogOut(LogoutToken);
    }

    [WebMethod]
    public MessagesEntity[] GetRecords(int pageNumber)
    {
        Token objToken = LoginAndGetToken();

        Pager objPager = new Pager();
        objPager.PageNumber = (pageNumber + 1);
        objPager.RowsPerPage = 100;

        MessageBoard_1 CloudApp = new MessageBoard_1();
        MessagesEntityListResponse colMessagesEntityListResponse = 
            CloudApp.GetMessagesList(objToken, 0, objPager);
        Logout(objToken);

        return colMessagesEntityListResponse.Result;
    }

    [WebMethod]
    public void SaveRecord(string Name, string Message)
    {
        Token objToken = LoginAndGetToken();

        MessagesEntity[] objMessagesEntity = new MessagesEntity[1];
        objMessagesEntity[0] = new MessagesEntity();
        objMessagesEntity[0].nameText = Name;
        objMessagesEntity[0].messageText = Message;

        MessageBoard_1 CloudApp = new MessageBoard_1();
        CloudApp.SaveMessages(objToken, objMessagesEntity);

        Logout(objToken);
    }

}

