using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using foxit;
using foxit.pdf;
using foxit.common.fxcrt;
using foxit.pdf.interform;
using System.Drawing;
using System.Windows.Threading;
using System.Windows.Forms;

namespace PDFReader_Demo_CSharp.UI
{
    public class FormFillerAssist : FillerAssistCallback
    {
        protected CPDFReaderDoc reader_doc_;
        private System.Windows.Threading.DispatcherTimer timer_ = new DispatcherTimer();
        public FormFillerAssist(CPDFReaderDoc reader_doc)
        {
            reader_doc_ = reader_doc;
        }

        public override void Dispose()
        {
            base.Dispose();
        }

        public override void FocusGotOnControl(foxit.pdf.interform.Control control, string value)
        {

        }

        public override void FocusLostFromControl(foxit.pdf.interform.Control control, string value)
        {

        }

        public override int GetVersion()
        {
            return 1;
        }

        public override bool KillTimer(int timer_id)
        {
            if(timer_id == 1)
                timer_.Stop();
            return true;
        }

        public override void Refresh(PDFPage page, RectF rect)
        {
            MainForm main_form = reader_doc_.GetView();
            if (main_form == null)
                return;
            main_form.ReDraw();
        }

        public override void Release()
        {
            timer_.Stop();
            MainForm main_form = reader_doc_.GetView();
            if (main_form != null)
            {
                main_form.SetTimeCallback(null);
            }
           
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            MainForm main_form = reader_doc_.GetView();
            if (main_form == null)
                return ;
            if(main_form.GetTimeCallback()!=null)
                main_form.GetTimeCallback().OnTimer(1);
        }

        public override bool SetTimerCallback(int elapse, TimerCallback timer, out int out_timer_id)
        {
            MainForm main_form = reader_doc_.GetView();
            if (main_form == null)
            {
                out_timer_id = -1;
                return false;
            }
            main_form.SetTimeCallback(timer);
            out_timer_id = 1;
            timer_.Interval = new System.TimeSpan(0,0,0,0,elapse);
            timer_.Tick += new EventHandler(Timer_Tick);
            timer_.Start();
            return true;
        }

        public override bool AppendPopupMenuItem(IntPtr h_menu, PopupMenuItem id_item, string default_string) {
            MainForm main_form = reader_doc_.GetView();
            if (main_form == null) return false;
            ContextMenuStrip contextMenuStrip = (ContextMenuStrip)ContextMenuStrip.FromHandle(h_menu);

            ToolStripMenuItem strip_menuitem = new ToolStripMenuItem();
            strip_menuitem.Size = new System.Drawing.Size(106, 22);
            strip_menuitem.Name = id_item.ToString();
            strip_menuitem.Text = default_string;
            switch (id_item) {
                case PopupMenuItem.e_PopupMenuItemCut:
                    strip_menuitem.Click += new System.EventHandler(main_form.OnFormFillerCut);
                    break;
                case PopupMenuItem.e_PopupMenuItemCopy:
                    strip_menuitem.Click += new System.EventHandler(main_form.OnFormFillerCopy);
                    break;
                case PopupMenuItem.e_PopupMenuItemPaste:
                    strip_menuitem.Click += new System.EventHandler(main_form.OnFormFillerPaste);
                    break;
                case PopupMenuItem.e_PopupMenuItemDelete:
                    strip_menuitem.Click += new System.EventHandler(main_form.OnFormFillerDelete);
                    break;
                case PopupMenuItem.e_PopupMenuItemSelectAll:
                    strip_menuitem.Click += new System.EventHandler(main_form.OnFormFillerSelectAll);
                    break;
                default:
                    break;
            }
            
            contextMenuStrip.Items.Add(strip_menuitem);
            return true;
        }

        public override IntPtr CreatePopupMenu() {
            MainForm main_form = reader_doc_.GetView();
            if (main_form == null) return IntPtr.Zero;
            ContextMenuStrip contextmenu = main_form.CreateFormFillerMenu();
            return contextmenu.Handle;
        }

        public override void DestroyPopupMenu(IntPtr h_menu) {
        }

        public override bool EnablePopupMenuItem(IntPtr h_menu, PopupMenuItem menu_item, bool is_enabled) {
            ContextMenuStrip contextMenuStrip = (ContextMenuStrip)ContextMenuStrip.FromHandle(h_menu);
            ToolStripMenuItem item = null;
            foreach (object x in contextMenuStrip.Items)
            {
                if (x.GetType().Name == "ToolStripMenuItem")
                {
                    item = (ToolStripMenuItem)x;
                    if (item.Name == menu_item.ToString()) break;
                }
            }
            if (item != null)
              item.Enabled = is_enabled; 
            return true;
        }

        public override bool ShowPopupMenu(IntPtr h_menu, foxit.common.fxcrt.PointF point) {
            MainForm main_form = reader_doc_.GetView();
            if (main_form == null || main_form.Viewer == null)
                return false;

            System.Drawing.Point pointUI = new System.Drawing.Point();
            main_form.Viewer.DocToWindow(reader_doc_.GetCurPage(), point, ref pointUI);
            main_form.ShowFormFillerPopup(h_menu, pointUI);
            return true;
        }
		
		public override void SetClipboardText(string text) {
			
		}
		
		public override string GetClipboardText() { return ""; }
        public override void ReportInvalidValue(string form_field_name, string invalid_value) {
            MessageBox.Show("The value " + "\"" + invalid_value + "\"" + " entered, named " + "\""+ form_field_name + "\""+ ", is illegal.", "ReportInvalidValue");
        }
}
}
