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;
using iTripLog.Edit;

namespace iTripLog.Upload
{
    public partial class SnapForm : 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 bool gpsOpen = false;

        public string getFileName()
        {
            return imagefilename;
        }

        public SnapForm()
        {
            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;          
            if (gps.Opened == false)
            {
                gps.Open();
                gpsOpen = true;
            }    
            GpsDeviceState state = gps.GetDeviceState();
            GpsPosition pos = gps.GetPosition();
            if (pos.LatitudeValid)
            {
                picLatitude = pos.Latitude;
                lbl_location.Text = "lati-" + picLatitude;
            }
            if (pos.LongitudeValid)
            {
                picLongitude = pos.Longitude;
                lbl_location.Text = " longi-" + picLongitude;
            }
            using (CameraCaptureDialog dlg = new CameraCaptureDialog())
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    picBox.Image = new Bitmap(dlg.FileName);
                    this.imagefilename = dlg.FileName;
                }
            }

            if (!this.gpsOpen)
                this.btn_gps.ButtonImage = this.btn_gps.ClickImage;
            
                
        }

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

            string title = txt_title.Text;
            string description = txt_tag.Text;
            string photoId = flickr.UploadPicture(imagefilename, title, description, "", uploadAsPublic, false, false);

            PhotoSearchOptions userSearch = new PhotoSearchOptions();
            FoundUser user = flickr.PeopleFindByUsername("hychyc07");
            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)
                {
                    flickr.PhotosGeoSetLocation(photoId,picLatitude,picLongitude);
                    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 btn_back_Click(object sender, EventArgs e)
        {
            if (gps.Opened == false)
            {
                gps.Open();
                gpsOpen = true;
            }
            GpsDeviceState state = gps.GetDeviceState();
            GpsPosition pos = gps.GetPosition();
            if (pos.LatitudeValid)
            {
                picLatitude = pos.Latitude;
            }
            if (pos.LongitudeValid)
            {
                picLongitude = pos.Longitude;
            }
            using (CameraCaptureDialog dlg = new CameraCaptureDialog())
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    picBox.Image = new Bitmap(dlg.FileName);
                    this.imagefilename = dlg.FileName;
                }
            }
        }

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

        private void btn_setting_Click(object sender, EventArgs e)
        {
            Setting setform = new Setting();
            setform.Show();
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            this.editor = new Editor();
            editor.changeBackground(imagefilename);
            this.editor.Show();
        }

        private void lbl_location_ParentChanged(object sender, EventArgs e)
        {

        }

      


    }
}