using System;
using System.Collections;
using System.Collections.Generic;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;

namespace JuSNS.Common
{
    /// <summary>
    /// Gtalk联系人
    /// </summary>
    public class Gtalk
    {
        private ContactsService service;
        private List<string> _linkmans;

        public Gtalk()
        {
            _linkmans = new List<string>();
        }

        /// <summary>
        /// 登录Gtalk
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="UserPassword">密码</param>
        public int Connect(string UserName, string UserPassword)
        {
            int n = 0;
            try
            {
                service = new ContactsService("exampleCo-exampleApp-1");
                service.setUserCredentials(UserName, UserPassword);
                GetLinkmans();
            }
            catch
            {
                n = 1;
            }
            return n;
        }
        
        /// <summary>
        /// 联系人列表
        /// </summary>
        public List<string> Linkmans
        {
            get { return _linkmans; }
        }

        /// <summary>
        /// 取得联系人列表
        /// </summary>
        private void GetLinkmans()
        {
            _linkmans.Clear();
            ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
            ContactsFeed feed = service.Query(query);
            foreach (ContactEntry entry in feed.Entries)
            {
                foreach (Google.GData.Extensions.EMail email in entry.Emails)
                {
                    _linkmans.Add(email.Address);
                }
            }
        }
    }
}
