﻿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 LinkFuture.Extensions;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
using System.ServiceModel.Configuration;

namespace SLDemo
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            DemoService.PSCServiceClient client = new DemoService.PSCServiceClient();
            client.LoginCompleted += delegate(object cs, DemoService.LoginCompletedEventArgs ce)
            {
                MessageBox.Show(ce.Result.ToString());
            };
            client.LoginAsync("cyokin", "pass");
        }

        private void btnLogout_Click(object sender, RoutedEventArgs e)
        {
            DemoService.PSCServiceClient client = new DemoService.PSCServiceClient();
            client.LogoutCompleted += delegate(object cs, System.ComponentModel.AsyncCompletedEventArgs ce)
            {
                MessageBox.Show("Logout");
            };
            client.LogoutAsync();
        }


        private void btnStatus_Click(object sender, RoutedEventArgs e)
        {
            DemoService.PSCServiceClient client = new DemoService.PSCServiceClient();
            client.IsLoginedCompleted += delegate(object cs, DemoService.IsLoginedCompletedEventArgs ce)
            {
                MessageBox.Show("IsLogined" + ce.Result.ToString());
            };
            client.IsLoginedAsync();
        }

        void client_IsLoginedCompleted(object sender, DemoService.IsLoginedCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void btnError_Click(object sender, RoutedEventArgs e)
        {
            DemoService.PSCServiceClient client = new DemoService.PSCServiceClient();
            client.DoWorkCompleted += delegate(object cs, DemoService.DoWorkCompletedEventArgs ce)
            {
                if (ce.Error == null)
                {
                    MessageBox.Show(ce.Result.DataContractToXmlString());
                }
                else
                {
                    MessageBox.Show(ce.Error.Message);
                }
            };
            client.DoWorkAsync("", "");
        }

        void client_DoWorkCompleted(object sender, DemoService.DoWorkCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void btnSuccess_Click(object sender, RoutedEventArgs e)
        {
            DemoService.PSCServiceClient client = new DemoService.PSCServiceClient();
            client.DoWorkCompleted += delegate(object cs, DemoService.DoWorkCompletedEventArgs ce)
            {
                if (ce.Error == null)
                {
                    MessageBox.Show(ce.Result.DataContractToXmlString());
                }
                else
                {
                    MessageBox.Show(ce.Error.Message);
                }
            };
            client.DoWorkAsync("1", "2");
        }

        private void btnHeader_Click(object sender, RoutedEventArgs e)
        {
            DemoService.PSCServiceClient client = new DemoService.PSCServiceClient();
            string headerData = "AAAA";
            using (OperationContextScope scope = new System.ServiceModel.OperationContextScope(client.InnerChannel))
            {
                MessageHeader head = MessageHeader.CreateHeader(
                    "Authorization",
                    "",
                    headerData);
                OperationContext.Current.OutgoingMessageHeaders.Add(head);

                client.DoHeadWorkCompleted += delegate(object cs, DemoService.DoHeadWorkCompletedEventArgs ce)
                {
                    if (ce.Error == null)
                    {
                        MessageBox.Show(ce.Result.DataContractToXmlString());
                    }
                    else
                    {
                        MessageBox.Show(ce.Error.Message);
                    }
                };
                client.DoHeadWorkAsync("1", "2");
            }
            

            //SLDemo.DemoService.IPSCService proxy = (SLDemo.DemoService.IPSCService)client;

            //using (OperationContextScope ocs = new OperationContextScope(client.InnerChannel)) { 
            //    var ser = new HeaderSerializer(); 
            //    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("SecurityHeader", "http://somesite.com/schema", "aa", ser)); 
            //    var req = new GetPredefinedSearchResultsRequest() { Someproperty = somevalue }; 
            //    client.GetPredefinedSearchResultsAsync(req); 
            //}

        }

    }
}
