using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using LausWPConverter.Data;

namespace LausWPConverter
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void Log(string msg)
        {
            logTextBox.AppendText(msg + (char)13 + (char)10);
            logTextBox.SelectionStart = logTextBox.Text.Length;
            logTextBox.ScrollToCaret();
        }

        private void DownloadFiles(LausDataSet.articleRow artRow)
        {
            fileTableAdapter.FillByIDArticle(lausDataSet.file, artRow.id);


            foreach (LausDataSet.fileRow fRow in lausDataSet.file)
            {

                string fileUrl = "";
                if (fRow.id_filetype == "img")
                {
                    fileUrl = "http://www.dulist.hr/uploads/" + fRow.created.ToString("yyyyMMdd") + "/large/" + fRow.path;                                    
                } else
                {
                    fileUrl = "http://www.dulist.hr/uploads/" + fRow.created.ToString("yyyyMMdd") + "/" + fRow.path;
                }

                string lfPath = "temp\\" + fRow.path;

                if (!File.Exists(lfPath))
                {
                    Log("\tDownload File: " + fileUrl);
                    Application.DoEvents();

                    using (WebClient wc = new WebClient())
                    {
                        wc.DownloadFile(fileUrl, lfPath);
                    }
                    
                    Log("\tDownload Complete: " + fileUrl);                    
                } else
                {
                    Log("\tFile Exists: " + fileUrl);
                    Application.DoEvents();
                }
            }
        }

        private void ConvertArticle(LausDataSet.articleRow artRow)
        {
            Log("Converting Article: ID=" + artRow.id.ToString());

            article_languageTableAdapter.FillByIDArticle(lausDataSet.article_language, artRow.id);
            LausDataSet.article_languageRow alRow = lausDataSet.article_language[0];

            wp_postsTableAdapter.FillByID(wpDataSet.wp_posts, artRow.id);
            WPDataSet.wp_postsRow postRow;
            if (wpDataSet.wp_posts.Count > 0)
            {
                postRow = wpDataSet.wp_posts[0];
            } else
            {
                postRow = wpDataSet.wp_posts.Newwp_postsRow();   
            }             
                        
            postRow.ID = (ulong)artRow.id;            
            postRow.post_author = 1;
            postRow.post_date = artRow.created;
            postRow.post_date_gmt = artRow.created;
            postRow.post_content = alRow.long_desc;
            postRow.post_title = alRow.title;
            postRow.post_excerpt = "";
            postRow.post_status = "publish";
            postRow.comment_status = "open";
            postRow.ping_status = "open";
            postRow.post_password = "";
            postRow.post_name = alRow.title;
            postRow.to_ping = "";
            postRow.pinged = "";
            postRow.post_modified = DateTime.Now;
            postRow.post_modified_gmt = DateTime.Now;
            postRow.post_content_filtered = "";
            postRow.post_parent = 0;
            postRow.guid = "http://www.dulist.hr/dev/?p=" + artRow.id.ToString();
            postRow.menu_order = 0;
            postRow.post_type = "post";
            postRow.post_mime_type = "";
            postRow.comment_count = 0;


            if (wpDataSet.wp_posts.Count == 0)
                wpDataSet.wp_posts.Addwp_postsRow(postRow);

            wp_postsTableAdapter.Update(wpDataSet.wp_posts);

            DownloadFiles(artRow);

            Log("Article Converted: ID=" + artRow.id.ToString());
        }

        private void convertButton_Click(object sender, EventArgs e)
        {
            lausDataSet.Clear();
            //articleTableAdapter.Fill(lausDataSet.article);
            articleTableAdapter.FillByID(lausDataSet.article, 14994);
            
            ConvertArticle(lausDataSet.article[0]);
        }
    }
}
