using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using foxit.pdf;

namespace PDFReader_Demo_CSharp.Common
{
    public class AppSetting
    {
        public static System.Drawing.Color select_text_color = System.Drawing.Color.FromArgb(0x20, 0x00, 0x00, 0xff);
        public static System.Drawing.Color search_text_color = System.Drawing.Color.FromArgb(0x95, 0xff, 0xff, 0x00);
        public static System.Drawing.Color document_background_color = System.Drawing.Color.FromArgb(0xff, 0xb2, 0xc8, 0xbb);
    }

    class FoxitUtil
    {
        public static PDFPage LoadPage(PDFDoc doc, int page_index, int parse_page_flag = 0)
        {
            if (doc.IsEmpty())
                return null;

            PDFPage page = doc.GetPage(page_index);
            if (page != null)
            {
                if (!page.IsParsed())
                    page.StartParse(parse_page_flag, null, false);
            }
            return page;
        }

        public static foxit.common.fxcrt.RectF TransformPageMatrixToRect(PDFPage page, int x, int y, float scale, foxit.common.Rotation rotate, foxit.common.fxcrt.RectF origin_rect)
        {
            Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
            float dpiX = graphics.DpiX;
            float dpiY = graphics.DpiY;

            float page_width = page.GetWidth() / 72.0f * dpiX;
            float page_height = page.GetHeight() / 72.0f * dpiY;

            float width = 0, height = 0;
            if (rotate == foxit.common.Rotation.e_Rotation0 || rotate == foxit.common.Rotation.e_Rotation180)
            {
                height = page_height * scale;
                width = page_width * scale;
            }
            else
            {
                height = page_width * scale;
                width = page_height * scale;
            }

            foxit.common.fxcrt.Matrix2D matrix = page.GetDisplayMatrix(x, y, (int)width, (int)height, rotate);
            foxit.common.fxcrt.RectF dest_rect = new foxit.common.fxcrt.RectF(origin_rect.left, origin_rect.bottom, origin_rect.right, origin_rect.top);
            dest_rect.Transform(matrix);

            return dest_rect;
        }

        public static foxit.common.fxcrt.PointF TransformPageMatrixToPointF(PDFPage page, int x, int y, float scale, foxit.common.Rotation rotate, foxit.common.fxcrt.PointF origin_point)
        {
            Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
            float dpiX = graphics.DpiX;
            float dpiY = graphics.DpiY;

            float page_width = page.GetWidth() / 72.0f * dpiX;
            float page_height = page.GetHeight() / 72.0f * dpiY;

            float width = 0, height = 0;
            if (rotate == foxit.common.Rotation.e_Rotation0 || rotate == foxit.common.Rotation.e_Rotation180)
            {
                height = page_height * scale;
                width = page_width * scale;
            }
            else
            {
                height = page_width * scale;
                width = page_height * scale;
            }

            foxit.common.fxcrt.Matrix2D matrix = page.GetDisplayMatrix(x, y, (int)(width + 0.5), (int)(height + 0.5), rotate);
            foxit.common.fxcrt.Matrix2D reverse_matrix = new foxit.common.fxcrt.Matrix2D();
            reverse_matrix.SetReverse(matrix);
            float pos_x = origin_point.x;
            float pos_y = origin_point.y;
            reverse_matrix.TransformPoint(ref pos_x, ref pos_y);

            return new foxit.common.fxcrt.PointF(pos_x, pos_y);
        }
    }
}
