using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Forms;
using FlickrNet;
using XTransMobile;

namespace iTripLog.Download
{
    public partial class DownloadForm : XTransMobile.XTForm
    {
        Flickr flickr;


        public DownloadForm(Flickr flickr)
        {
            InitializeComponent();
            this.flickr = flickr;

        }

        private void btn_findPhotos_Click(object sender, EventArgs e)
        {
            string screenName = txt_userName.Text; 
            FoundUser user = flickr.PeopleFindByUsername(screenName); 
            string userId = user.UserId;

            // First page of the users photos
            // Sorted by interestingness
            PhotoSearchOptions userSearch = new PhotoSearchOptions ();
            userSearch.UserId = userId;
            userSearch.SortOrder = PhotoSearchSortOrder.InterestingnessAsc;
            Photos usersPhotos = flickr.PhotosSearch(userSearch);
            // Get users contacts
            Contacts contacts = flickr.ContactsGetPublicList(userId);
            // Get first page of a users favorites
            Photos usersFavoritePhotos = flickr.FavoritesGetPublicList(userId);
            // Get a list of the users groups
            PublicGroupInfo[] usersGroups = flickr.PeopleGetPublicGroups(userId);

            Photo[] photo = usersPhotos.PhotoCollection;
            int y_align = 120;
            for (int i = 0; i < photo.Length; i++)
            {
                System.IO.Stream downStream = flickr.DownloadPicture(photo[i].SmallUrl);
                PictureBox picBox = new PictureBox();
                picBox.Name = "picBox" + i;
                picBox.Location = new System.Drawing.Point(24, y_align+i*160);
                picBox.Size = new System.Drawing.Size(189, 120);
                this.Controls.Add(picBox);    
                Bitmap bm = new Bitmap(downStream);
                picBox.Image = Image.FromHbitmap(bm.GetHbitmap());
                ImageInfo imagef = new ImageInfo(picBox.Image);
                imagef.ImageName = picBox.Name;
                imagef.Latitude = decimal.ToDouble(photo[i].Latitude);
                imagef.Longitude = decimal.ToDouble(photo[i].Longitude);
                picBox.Tag = imagef;
                picBox.Click += new EventHandler(this.picBox_OnClick);
            }
        }

        private void picBox_OnClick(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            ShowImageForm sif = new ShowImageForm(pb);
            sif.Show();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}