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 Microsoft.WindowsMobile.Samples.Location;
using FlickrNet;
using System.IO;
using XTransMobile;

namespace iTripLog.Download
{
    public partial class Form1 : XTransMobile.XTForm
    {

        private Flickr flickr;   
        private string settingfile;   
        private Settings settings;
        private string imagefilename;
        private Gps gps;
        private static double picLatitude;
        private static double picLongitude;
//        private static string strLatitude = "";
//        private static string strLongitude = "";

        public Form1()
        {
            InitializeComponent();
            flickr = new Flickr("23589c82ce57562f2dbe6f9810b81a99", "529608c61cd85236","72157622141823959-460ef5327a4ad8d5"); 
            gps = new Gps();
            settingfile = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName),"settings.xml");   
            settings = Settings.Load(settingfile);   
            if (string.IsNullOrEmpty(settings.FlickrAuthToken) == false)   
                flickr.AuthToken = settings.FlickrAuthToken;          
        }

        private void btnCamera_Click(object sender, EventArgs e)
        {
            if (gps.Opened == false)
                gps.Open();
            GpsDeviceState state = gps.GetDeviceState();
            GpsPosition pos = gps.GetPosition();
            if (pos.LatitudeValid)
            {
 //               strLatitude = (pos.Latitude > 0) ? "东经" : "西经" + pos.LatitudeInDegreesMinutesSeconds;
                picLatitude = pos.Latitude;
            }
            if (pos.LongitudeValid)
            {
//                strLongitude = ((pos.Longitude > 0) ? "北纬 ":"南纬 ")+pos.LongitudeInDegreesMinutesSeconds;
                picLongitude = pos.Longitude;
            }
            using (CameraCaptureDialog dlg = new CameraCaptureDialog())   
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    pictureBox1.Image = new Bitmap(dlg.FileName);
                    this.imagefilename = dlg.FileName;
                }      
            }
        }
        private void menuItem2_Click(object sender, EventArgs e)
        {
            Close();
            if (gps.Opened == true)
                gps.Close();
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            string tempFrob = flickr.AuthGetFrob();   
            string flickrUrl = flickr.AuthCalcUrl(tempFrob, AuthLevel.Write);   
 
            System.Diagnostics.Process.Start(flickrUrl, string.Empty);   
  
            MessageBox.Show("After Authorization from Broswer, Please Close the Window");     
            try  
            {   
                Auth auth = flickr.AuthGetToken(tempFrob);   
                settings.FlickrAuthToken = auth.Token;   
                settings.Save(settingfile);   
                flickr.AuthToken = auth.Token;   
            }   
            catch (FlickrException ex)   
            {   
                MessageBox.Show(ex.ToString());   
            }
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(flickr.AuthToken) == true)   
            {   
                MessageBox.Show("Not Authorization");   
                return;   
            }
            bool uploadAsPublic = true;

            string title = "CameraNow";
            string description = picLatitude+" "+picLongitude;
            string photoId = flickr.UploadPicture(imagefilename, title, description, "", uploadAsPublic, false, false);
            
            PhotoSearchOptions userSearch = new PhotoSearchOptions();
            FoundUser user = flickr.PeopleFindByUsername("celiacxfly"); 
            string userId = user.UserId;
            userSearch.UserId = userId;
            Photos usersPhotos = flickr.PhotosSearch(userSearch);
            Photo[] photo = usersPhotos.PhotoCollection;
            for (int i = 0; i < photo.Length; i++)
            {
                if (photo[i].PhotoId == photoId)
                {
                    photo[i].Latitude = (decimal)picLatitude;
                    photo[i].Longitude =(decimal)picLongitude;
                }
            }
           



            string s = "Photo uploaded Successfully\r\n";
            s += "Photo Id = " + photoId + "\r\n";
            MessageBox.Show(s); 
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            DownloadForm df = new DownloadForm(flickr);
            df.Show();
        }
  }
}