using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CloudDBSilverlight.cloudDBService;
using System.Collections;
using System.ServiceModel;

namespace CloudDBSilverlight
{
    public partial class MainPage : UserControl
    {
        int currentPage;
        List<Message> colMessages = new List<Message>();

        public class Message
        {
            public string Name { get; set; }
            public string Comment { get; set; }
        }
 
        public MainPage()
        {
            InitializeComponent();
            currentPage = 1;
            GetRecords(currentPage);        
        }

        private string GetWebserviceAddress()
        {
            string strXapFile = @"/ClientBin/CloudDBSilverlight.xap";
            string strBaseWebAddress = App.Current.Host.Source.AbsoluteUri.Replace(strXapFile, "");
            return string.Format(@"{0}/{1}", strBaseWebAddress, "WebService.asmx"); 
        }

        private void GetRecords(int PageNumber)
        {
            WebServiceSoapClient CloudDBApp = new WebServiceSoapClient();
            EndpointAddress WebServiceEndpointAddress = new EndpointAddress(GetWebserviceAddress());
            CloudDBApp.Endpoint.Address = WebServiceEndpointAddress;
            CloudDBApp.GetRecordsCompleted +=
                new EventHandler<GetRecordsCompletedEventArgs>(CloudDBApp_GetRecordsCompleted);
            CloudDBApp.GetRecordsAsync(PageNumber);
            txtBusy.Visibility = Visibility.Visible;     
        }

        void CloudDBApp_GetRecordsCompleted(object sender, GetRecordsCompletedEventArgs e)
        {
            txtBusy.Visibility = Visibility.Collapsed;     
            colMessages = new List<Message>();
            foreach (var item in e.Result)
            {
                Message objMessage = new Message();

                objMessage.Name = item.nameText;
                objMessage.Comment = item.messageText;

                colMessages.Add(objMessage);
            }

            dgMessages.ItemsSource = colMessages;
        }

        private void btnLeft_Click(object sender, RoutedEventArgs e)
        {
            if (currentPage > 1)
            {
                currentPage--;
                GetRecords(currentPage);
            }
        }

        private void btnRight_Click(object sender, RoutedEventArgs e)
        {
            if (colMessages.Count > 0)
            {
                currentPage++;
                GetRecords(currentPage);
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            WebServiceSoapClient CloudDBApp = new WebServiceSoapClient();
            EndpointAddress WebServiceEndpointAddress = new EndpointAddress(GetWebserviceAddress());
            CloudDBApp.Endpoint.Address = WebServiceEndpointAddress;
            CloudDBApp.SaveRecordCompleted += 
                new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(CloudDBApp_SaveRecordCompleted);
            CloudDBApp.SaveRecordAsync(txtName.Text, txtMessage.Text);
            txtName.Text = "";
            txtMessage.Text = "";
            txtBusy.Visibility = Visibility.Visible;        
        }

        void CloudDBApp_SaveRecordCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            txtBusy.Visibility = Visibility.Collapsed; 
            GetRecords(currentPage);
        }
    }
}
