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;

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

        public SecurityTabPage()
        {
            InitializeComponent();
        }

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

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

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

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

            PDFDoc pdf_doc = reader_doc.GetPDFDocument();
            PDFDoc.EncryptType encryption_type = pdf_doc.GetEncryptionType();
            string encryption_type_string = "";
            switch (encryption_type)
            {
                case PDFDoc.EncryptType.e_EncryptUnknown:
                    encryption_type_string = "Unknown";
                    break;
                case PDFDoc.EncryptType.e_EncryptNone:
                    encryption_type_string = "No";
                    break;
                case PDFDoc.EncryptType.e_EncryptPassword:
                    encryption_type_string = "Password";
                    break;
                case PDFDoc.EncryptType.e_EncryptCertificate:
                    encryption_type_string = "Digital certificate";
                    break;
                case PDFDoc.EncryptType.e_EncryptFoxitDRM:
                    encryption_type_string = "Foxit DRM";
                    break;
                case PDFDoc.EncryptType.e_EncryptCustom:
                    encryption_type_string = "Customized";
                    break;
                case PDFDoc.EncryptType.e_EncryptRMS:
                    encryption_type_string = "Microsoft RMS";
                    break;
            }

            securityMothodComboBox.Text = encryption_type_string;

            PDFDoc.PasswordType password_type = pdf_doc.GetPasswordType();
            string password_type_string = "";
            switch (password_type)
            {
                case PDFDoc.PasswordType.e_PwdInvalid:
                    password_type_string = "Invalid";
                    break;
                case PDFDoc.PasswordType.e_PwdNoPassword:
                    password_type_string = "No password";
                    break;
                case PDFDoc.PasswordType.e_PwdUser:
                    password_type_string = "User password";
                    break;
                case PDFDoc.PasswordType.e_PwdOwner:
                    password_type_string = "Owner password";
                    break;
            }
            securityMethodValueLabel.Text = password_type_string;
            if (password_type == PDFDoc.PasswordType.e_PwdUser)
            {
                userPasswordValuelabel.Text = "Yes";
                ownerPasswordValueLabel.Text = "No";
            }
            else if (password_type == PDFDoc.PasswordType.e_PwdOwner)
            {
                userPasswordValuelabel.Text = "No";
                ownerPasswordValueLabel.Text = "Yes";
            }
            
            PDFDoc.UserPermissions permissions = (PDFDoc.UserPermissions)pdf_doc.GetUserPermissions();

            printValueLabel.Text = (permissions & PDFDoc.UserPermissions.e_PermPrint) == PDFDoc.UserPermissions.e_PermPrint ? "Allowed" : "None";
            if ((permissions & PDFDoc.UserPermissions.e_PermPrintHigh) == PDFDoc.UserPermissions.e_PermPrintHigh)
                printValueLabel.Text = "High Resolution Print";

            modifyDocumentValueLabel.Text = (password_type == PDFDoc.PasswordType.e_PwdOwner || ((permissions & PDFDoc.UserPermissions.e_PermModify) == PDFDoc.UserPermissions.e_PermModify)) ? "Allowed" : "None";
            fillinFormsValueLabel.Text = (password_type == PDFDoc.PasswordType.e_PwdOwner || ((permissions & PDFDoc.UserPermissions.e_PermExtract) == PDFDoc.UserPermissions.e_PermExtract)) ? "Allowed" : "None";
            contentExtractionValueLabel.Text = (password_type == PDFDoc.PasswordType.e_PwdOwner || ((permissions & PDFDoc.UserPermissions.e_PermFillForm) == PDFDoc.UserPermissions.e_PermFillForm)) ? "Allowed" : "None";
            documentAssemblyValueLabel.Text = (password_type == PDFDoc.PasswordType.e_PwdOwner || ((permissions & PDFDoc.UserPermissions.e_PermAssemble) == PDFDoc.UserPermissions.e_PermAssemble)) ? "Allowed" : "None";
        }
    }
}
