using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using foxit.pdf;
using foxit.addon.xfa;

namespace PDFReader_Demo_CSharp.UI
{
    public partial class CreationInfoTabPage : UserControl
    {
        private MainForm main_form_ = null;

        public CreationInfoTabPage()
        {
            InitializeComponent();
        }

        public void SetHostedPage(MainForm main_form)
        {
            main_form_ = main_form;
        }

        private void Page_OnLoad(object sender, EventArgs e)
        {
            UpdateControl();
        }

        string GetMetaDataString(Metadata metadata, string key)
        {
            string ret_string = "";

            foxit.common.WStringArray metadata_value_array = metadata.GetValues(key);
            uint array_size = metadata_value_array.GetSize();
            for (uint i = 0; i < array_size; i++)
            {
                ret_string += metadata_value_array.GetAt(i);
                if (i != array_size - 1 && key == "Keywords")
                    ret_string += ", ";
            }

            return ret_string;
        }

        void UpdateControl()
        {
            if (main_form_ == null)
                return;

            CPDFReaderDoc reader_doc = main_form_.GetCurDocument();
            if (reader_doc == null)
                return;

            PDFDoc pdf_doc = reader_doc.GetPDFDocument();
            int current_page_index = main_form_.GetCurPageIndex();
            string file_path = reader_doc.GetFilePath();
            filePathValueTextBox.Text = file_path;

            Metadata metadata = new Metadata(pdf_doc);

            foxit.common.DateTime creation_time = metadata.GetCreationDateTime();
            string creation_time_string = string.Format("{0:d4}-{1:d2}-{2:d2} {3:d2}:{4:d2}:{5:d2}  {6}{7}'{8}'", creation_time.year, creation_time.month, creation_time.day,
                creation_time.hour, creation_time.minute, creation_time.second, creation_time.utc_hour_offset > 0 ? "+" : "-",
                creation_time.utc_hour_offset,
                creation_time.utc_minute_offset);
            createdValueLabel.Text = creation_time_string;

            foxit.common.DateTime modified_time = metadata.GetModifiedDateTime();
            string modified_time_string = string.Format("{0:d4}-{1:d2}-{2:d2} {3:d2}:{4:d2}:{5:d2} {6}{7}'{8}'", modified_time.year, modified_time.month, modified_time.day,
                modified_time.hour, modified_time.minute, modified_time.second, modified_time.utc_hour_offset > 0 ? "+" : "-",
                modified_time.utc_hour_offset,
                modified_time.utc_minute_offset);
            modifiedValueLabel.Text = modified_time_string;

            titleValueTextBox.Text = GetMetaDataString(metadata, "Title");
            authorValueTextBox.Text = GetMetaDataString(metadata, "Author");
            subjectValueTextBox.Text = GetMetaDataString(metadata, "Subject");
            keywordsValueTextBox.Text = GetMetaDataString(metadata, "Keywords");
            creatorValueLabel.Text = GetMetaDataString(metadata, "Creator");
            producerValueLabel.Text = GetMetaDataString(metadata, "Producer");

            int file_size = pdf_doc.GetFileSize();
            filesizeValueLabel.Text = string.Format("{0:f2}MB({1}Bytes)", file_size / 1024.0f / 1024.0f, file_size);

            CReader_Page reader_page = reader_doc.GetPage(current_page_index);

            
            float page_width = 0.0f, page_height = 0.0f;
            if (reader_doc.IsDynamicXFA() == false)
            {
                PDFPage pdf_page = reader_page.GetPDFPage();
                page_width = pdf_page.GetWidth() / 72.0f;
                page_height = pdf_page.GetHeight() / 72.0f;
                pageCountValueLabel.Text = string.Format("{0}", pdf_doc.GetPageCount());
            } else {
                XFAPage pdf_page = reader_page.GetXFAPage();
                page_width = pdf_page.GetWidth() / 72.0f;
                page_height = pdf_page.GetHeight() / 72.0f;
                pageCountValueLabel.Text = string.Format("{0}", reader_doc.GetXFADoc().GetPageCount());
            }

            pagesizeValueLabel.Text = string.Format("{0:f2} x {1:f2} inch", page_width, page_height);
            metadata.Dispose();
        }
    }
}
