using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Globalization;
using System.Windows.Forms;
using O2S.Components.PDFView4NET;
using O2S.Components.PDFView4NET.Annotations;
using Annotations;
using O2S.Components.PDFView4NET.Graphics.Fonts;
using O2S.Components.PDFRender4NET;
using System.Drawing.Imaging;

namespace O2S.Samples.PDFView4NET.Annotations
{
    public partial class AnnotationsForm : Form
    {
        private PDFAnnotation selectedAnnotation;

        public AnnotationsForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            cbxViewerAction.Items.AddRange(Enum.GetNames(typeof(UserInteractiveWorkMode)));
            cbxViewerAction.SelectedIndex = 0;
            pdfDocument.FilePath = "..\\SupportFiles\\annotations.pdf";
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtPDFFile.Text = ofd.FileName;
                pdfDocument.FilePath = ofd.FileName;
            }
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            pdfPageView.GoToFirstPage();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            pdfPageView.GoToPrevPage();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            pdfPageView.GoToNextPage();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            pdfPageView.GoToLastPage();
        }

        private void cbxZoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            pdfPageView.Zoom = float.Parse(cbxZoom.Text, CultureInfo.InvariantCulture);
        }

        private void btnPanAndScan_Click(object sender, EventArgs e)
        {
            pdfPageView.WorkMode = UserInteractiveWorkMode.PanAndScan;
        }

        private void btnEditAnnotations_Click(object sender, EventArgs e)
        {
            pdfPageView.WorkMode = UserInteractiveWorkMode.EditAnnotations;
        }

        private void btnAddTextAnnotation_Click(object sender, EventArgs e)
        {
            pdfPageView.WorkMode = UserInteractiveWorkMode.AddTextAnnotation;
        }

        private void btnAddStampAnnotation_Click(object sender, EventArgs e)
        {
            pdfPageView.WorkMode = UserInteractiveWorkMode.AddStampAnnotation;
        }

        private void btnAddFileAnnotation_Click(object sender, EventArgs e)
        {
            pdfPageView.WorkMode = UserInteractiveWorkMode.AddFileAttachmentAnnotation;
        }

        private void btnAddInkAnnotation_Click(object sender, EventArgs e)
        {
            pdfPageView.WorkMode = UserInteractiveWorkMode.AddInkAnnotation;
        }

        private void btnAddLineAnnotation_Click(object sender, EventArgs e)
        {
            pdfPageView.WorkMode = UserInteractiveWorkMode.AddLineAnnotation;
        }

        private void btnAddRectangleAnnotation_Click(object sender, EventArgs e)
        {
            pdfPageView.WorkMode = UserInteractiveWorkMode.AddRectangleAnnotation;
        }

        private void btnAddCircleAnnotation_Click(object sender, EventArgs e)
        {
            pdfPageView.WorkMode = UserInteractiveWorkMode.AddEllipseAnnotation;
        }

        private void btnAddLinkAnnotation_Click(object sender, EventArgs e)
        {
            pdfPageView.WorkMode = UserInteractiveWorkMode.AddLinkAnnotation;
        }

        private void btnSaveDocument_Click(object sender, EventArgs e)
        {
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                pdfDocument.Save(sfd.FileName);
            }
        }

        private void pdfPageView_WorkModeChanged(object sender, EventArgs e)
        {
            btnPanAndScan.BackColor = 
                pdfPageView.WorkMode == UserInteractiveWorkMode.PanAndScan ? SystemColors.ControlDark : SystemColors.Control;
            btnEditAnnotations.BackColor =
                pdfPageView.WorkMode == UserInteractiveWorkMode.EditAnnotations ? SystemColors.ControlDark : SystemColors.Control;
            btnAddTextAnnotation.BackColor =
                pdfPageView.WorkMode == UserInteractiveWorkMode.AddTextAnnotation ? SystemColors.ControlDark : SystemColors.Control;
            btnAddStampAnnotation.BackColor =
                pdfPageView.WorkMode == UserInteractiveWorkMode.AddStampAnnotation ? SystemColors.ControlDark : SystemColors.Control;
            btnAddFileAnnotation.BackColor =
                pdfPageView.WorkMode == UserInteractiveWorkMode.AddFileAttachmentAnnotation ? SystemColors.ControlDark : SystemColors.Control;
            btnAddInkAnnotation.BackColor =
                pdfPageView.WorkMode == UserInteractiveWorkMode.AddInkAnnotation ? SystemColors.ControlDark : SystemColors.Control;
            btnAddLineAnnotation.BackColor =
                pdfPageView.WorkMode == UserInteractiveWorkMode.AddLineAnnotation ? SystemColors.ControlDark : SystemColors.Control;
            btnAddRectangleAnnotation.BackColor =
                pdfPageView.WorkMode == UserInteractiveWorkMode.AddRectangleAnnotation ? SystemColors.ControlDark : SystemColors.Control;
            btnAddCircleAnnotation.BackColor =
                pdfPageView.WorkMode == UserInteractiveWorkMode.AddEllipseAnnotation ? SystemColors.ControlDark : SystemColors.Control;
            btnAddLinkAnnotation.BackColor =
                pdfPageView.WorkMode == UserInteractiveWorkMode.AddLinkAnnotation ? SystemColors.ControlDark : SystemColors.Control;
        }

        private void pdfPageView_BeforeAnnotationAdd(object sender, BeforeAnnotationAddEventArgs e)
        {
            switch (e.Annotation.Type)
            {
                case PDFAnnotationType.Text:
                    PDFTextAnnotation ta = e.Annotation as PDFTextAnnotation;
                    EditTextAnnotationForm etaf = new EditTextAnnotationForm();
                    etaf.TextAnnotation = ta;

                    if (etaf.ShowDialog(this) != DialogResult.OK)
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        Bitmap appearance = new Bitmap("test.png");
                        ta.Appearance = new PDFImageAppearance(appearance);
                        appearance.Dispose();
                    }
                    break;
                case PDFAnnotationType.Stamp:
                    PDFStampAnnotation sa = e.Annotation as PDFStampAnnotation;
                    EditStampAnnotationForm esaf = new EditStampAnnotationForm();
                    esaf.StampAnnotation = sa;

                    if (esaf.ShowDialog(this) != DialogResult.OK)
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        Bitmap appearance = new Bitmap("test.png");
                        sa.Appearance = new PDFImageAppearance(appearance);
                        appearance.Dispose();
                    }
                    break;
                case PDFAnnotationType.FileAttachment:
                    PDFFileAttachmentAnnotation faa = e.Annotation as PDFFileAttachmentAnnotation;
                    EditFileAttachmentAnnotationForm efaaf = new EditFileAttachmentAnnotationForm();
                    efaaf.FileAttachmentAnnotation = faa;

                    if (efaaf.ShowDialog(this) != DialogResult.OK)
                    {
                        e.Cancel = true;
                    }
                    break;
                case PDFAnnotationType.Ink:
                    PDFInkAnnotation ia = e.Annotation as PDFInkAnnotation;
                    EditInkAnnotationForm eiaf = new EditInkAnnotationForm();
                    eiaf.InkAnnotation = ia;

                    if (eiaf.ShowDialog(this) != DialogResult.OK)
                    {
                        e.Cancel = true;
                    }
                    break;
                case PDFAnnotationType.Line:
                    PDFLineAnnotation la = e.Annotation as PDFLineAnnotation;
                    EditLineAnnotationForm elaf = new EditLineAnnotationForm();
                    elaf.LineAnnotation = la;

                    if (elaf.ShowDialog(this) != DialogResult.OK)
                    {
                        e.Cancel = true;
                    }
                    break;
                case PDFAnnotationType.Rectangle:
                    PDFRectangleAnnotation ra = e.Annotation as PDFRectangleAnnotation;
                    EditSCAnnotationForm eraf = new EditSCAnnotationForm();
                    eraf.SCAnnotation = ra;

                    if (eraf.ShowDialog(this) != DialogResult.OK)
                    {
                        e.Cancel = true;
                    }
                    break;
                case PDFAnnotationType.Ellipse:
                    PDFEllipseAnnotation ea = e.Annotation as PDFEllipseAnnotation;
                    EditSCAnnotationForm eeaf = new EditSCAnnotationForm();
                    eeaf.SCAnnotation = ea;

                    if (eeaf.ShowDialog(this) != DialogResult.OK)
                    {
                        e.Cancel = true;
                    }
                    break;
                case PDFAnnotationType.Link:
                    PDFLinkAnnotation lka = e.Annotation as PDFLinkAnnotation;
                    EditLinkAnnotationForm elkaf = new EditLinkAnnotationForm();
                    elkaf.LinkAnnotation = lka;

                    if (elkaf.ShowDialog(this) != DialogResult.OK)
                    {
                        e.Cancel = true;
                    }
                    break;
                case PDFAnnotationType.FreeText:
                    System.Drawing.Font batangFont = new Font("Batang", 12, FontStyle.Regular);
                    TrueTypeFont batangTT = new TrueTypeFont(batangFont, false);
                    batangFont.Dispose();

                    PDFFreeTextAnnotation fta = e.Annotation as PDFFreeTextAnnotation;
                    fta.Font = batangTT;
                    break;
            }
        }

        private void pdfPageView_AnnotationDoubleClick(object sender, AnnotationClickEventArgs e)
        {
            switch (e.Annotation.Type)
            {
                case PDFAnnotationType.Text:
                    PDFTextAnnotation ta = e.Annotation as PDFTextAnnotation;
                    EditTextAnnotationForm etaf = new EditTextAnnotationForm();
                    etaf.TextAnnotation = ta;
                    etaf.ShowDialog(this);
                    break;
                case PDFAnnotationType.Stamp:
                    PDFStampAnnotation sa = e.Annotation as PDFStampAnnotation;
                    EditStampAnnotationForm esaf = new EditStampAnnotationForm();
                    esaf.StampAnnotation = sa;
                    esaf.ShowDialog(this);
                    break;
                case PDFAnnotationType.FileAttachment:
                    PDFFileAttachmentAnnotation faa = e.Annotation as PDFFileAttachmentAnnotation;
                    EditFileAttachmentAnnotationForm efaaf = new EditFileAttachmentAnnotationForm();
                    efaaf.FileAttachmentAnnotation = faa;
                    efaaf.ShowDialog(this);
                    break;
                case PDFAnnotationType.Ink:
                    PDFInkAnnotation ia = e.Annotation as PDFInkAnnotation;
                    EditInkAnnotationForm eiaf = new EditInkAnnotationForm();
                    eiaf.InkAnnotation = ia;
                    eiaf.ShowDialog(this);
                    break;
                case PDFAnnotationType.Line:
                    PDFLineAnnotation la = e.Annotation as PDFLineAnnotation;
                    EditLineAnnotationForm elaf = new EditLineAnnotationForm();
                    elaf.LineAnnotation = la;
                    elaf.ShowDialog(this);
                    break;
                case PDFAnnotationType.Rectangle:
                    PDFRectangleAnnotation ra = e.Annotation as PDFRectangleAnnotation;
                    EditSCAnnotationForm eraf = new EditSCAnnotationForm();
                    eraf.SCAnnotation = ra;
                    eraf.ShowDialog(this);
                    break;
                case PDFAnnotationType.Ellipse:
                    PDFEllipseAnnotation ea = e.Annotation as PDFEllipseAnnotation;
                    EditSCAnnotationForm eeaf = new EditSCAnnotationForm();
                    eeaf.SCAnnotation = ea;
                    eeaf.ShowDialog(this);
                    break;
                case PDFAnnotationType.Link:
                    PDFLinkAnnotation lka = e.Annotation as PDFLinkAnnotation;
                    EditLinkAnnotationForm elkaf = new EditLinkAnnotationForm();
                    elkaf.LinkAnnotation = lka;
                    elkaf.ShowDialog(this);
                    break;
                default:
                    EditAnnotationForm eaf = new EditAnnotationForm();
                    eaf.Annotation = e.Annotation;
                    eaf.ShowDialog();
                    break;
            }
        }

        private void pdfPageView_AnnotationContextMenu(object sender, AnnotationContextMenuEventArgs e)
        {
            selectedAnnotation = e.Annotation;
            if (selectedAnnotation is PDFFileAttachmentAnnotation)
            {
                stdFileAnnotCtxMenu.Show(pdfPageView, e.MousePosition.X, e.MousePosition.Y);
            }
            else
            {
                stdAnnotCtxMenu.Show(pdfPageView, e.MousePosition.X, e.MousePosition.Y);
            }
        }

        private void showPopupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PDFMarkupAnnotation ma = selectedAnnotation as PDFMarkupAnnotation;
            if (ma != null)
            {
                ma.ShowPopup(true);
            }
        }

        private void deleteAnnotationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure you want to delete the selected annotation?",
                "PDFView4NET", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                selectedAnnotation.Page.Annotations.Remove(selectedAnnotation);
            }
        }

        private void saveFileToDiskToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PDFFileAttachmentAnnotation faa = selectedAnnotation as PDFFileAttachmentAnnotation;
            sfdFile.FileName = faa.FileName;
            if (sfdFile.ShowDialog() == DialogResult.OK)
            {
                faa.Save(sfdFile.FileName);
            }
        }

        private void pdfPageView_AnnotationClick(object sender, AnnotationClickEventArgs e)
        {

        }

        private void pdfPageView_PageNumberChanged(object sender, EventArgs e)
        {
            lblPageNumber.Text = "Page number: " + pdfPageView.PageNumber;
        }

        private void cbxViewerAction_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedValue = cbxViewerAction.SelectedItem.ToString();
            pdfPageView.WorkMode = (UserInteractiveWorkMode)Enum.Parse(typeof(UserInteractiveWorkMode), selectedValue, true);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            pdfPageView.SearchText(txtSearch.Text, O2S.Components.PDFView4NET.Text.PDFHighlightSearchResultsMode.AllResults);
        }

        private void btnGetPageImage_Click(object sender, EventArgs e)
        {
            PDFFile file = PDFFile.Open(txtPDFFile.Text);
            Bitmap pageImage = file.GetPageImage(pdfPageView.PageNumber, 300);
            pageImage.Save("pageimage.png", ImageFormat.Png);
            pageImage.Dispose();
            file.Dispose();
        }

        private void pdfPageView_AfterTextSelect(object sender, AfterTextSelectEventArgs e)
        {
            PDFHighlightAnnotation ha = new PDFHighlightAnnotation();
            ha.Contents = "xxx";
            ha.PDFRectangle = e.TextSelection.SelectedTextRuns[0].Bounds;

            pdfDocument.Pages[pdfPageView.PageNumber].Annotations.Add(ha);
        }
    }
}