using DEMO_FULL_CA_DOTNET_WEB.Models;
using Org.BouncyCastle.X509;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using ViettelFileSigner;

namespace DEMO_FULL_CA_DOTNET_WEB.Controllers
{
    public class MobileCAController : Controller
    {
        SignPdfFile pdfSig;
        string certViettelCABase64 = "MIIEKDCCAxCgAwIBAgIKYQ4N5gAAAAAAETANBgkqhkiG9w0BAQUFADB+MQswCQYDVQQGEwJWTjEzMDEGA1UEChMqTWluaXN0cnkgb2YgSW5mb3JtYXRpb24gYW5kIENvbW11bmljYXRpb25zMRswGQYDVQQLExJOYXRpb25hbCBDQSBDZW50ZXIxHTAbBgNVBAMTFE1JQyBOYXRpb25hbCBSb290IENBMB4XDTE1MTAwMjAyMzIyMFoXDTIwMTAwMjAyNDIyMFowOjELMAkGA1UEBhMCVk4xFjAUBgNVBAoTDVZpZXR0ZWwgR3JvdXAxEzARBgNVBAMTClZpZXR0ZWwtQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDLdiGZcPhwSm67IiLUWELaaol8kHF+qHPmEdcG0VDKf0FtpSWiE/t6NPzqqmoF4gbIrue1/TzUs7ZeAj28o6Lb2BllA/zB6YFrXfppD4jKqHMO139970MeTbDrhHTbVugX4t2QHS+B/p8+8lszJpuduBrnZ/LWxbhnjeQRr21g89nh/W5q1VbIvZnq4ci5m0aDiJ8arhK2CKpvNDWWQ5E0L7NTVoot8niv6/Wjz19yvUCYOKHYsq97y7eBaSYmpgJosD1VtnXqLG7x4POdb6Q073eWXQB0Sj1qJPrXtOqWsnnmzbbKMrnjsoE4gg9B6qLyQS4kRMp0RrUV0z041aUFAgMBAAGjgeswgegwCwYDVR0PBAQDAgGGMBIGA1UdEwEB/wQIMAYBAf8CAQAwHQYDVR0OBBYEFAhg5h8bFNlIgAtep1xzJSwgDfnWMB8GA1UdIwQYMBaAFM1iceRhvf497LJAYNOBdd06rGvGMDwG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uiKCrF2GJCZQipNs1DfMSyPggux3Z3hQ62JBuZfNvOJAkEAxIYCM5QMMHpe79Vrozc+k50nj+GKfTpOHeqajGUEI4K7x7IlMDmNqCC6t2A2dFA5/DCIHzosUeno6H6EZxjvQQJAY+IStgiUD0OEge4AU+0G/HzgAb5C5okmtfnj0j/9Y/3r3zgJiYGOuk3JJ6p3tc30brUYxGdyAtyvRx7eI8B3iQJBAJpa4qW6sJ36AKZFLq4D6EwaL2G3kc1bVFSwgRB0TFMB3Vak4O4mu1HWfgCWo20RvJCfcYCrIEdguvd3IunQ9Mc=";//Khai bao gia tri Prikey 
        string apID = "AP2";//Khai bao AppId

        public static String HASH_ALGORITHM_SHA_1 = "SHA1";
        public static String HASH_ALGORITHM_SHA_256 = "SHA256";
        //string ext = null;
        // GET: MobileCA
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult SignMobileCA()
        {

            ViewBag.statusResultSignMobileCA = "error";
            ViewBag.descErrorSignMobileCA = "";
            ViewBag.signedFileName = "";

            String phoneNumber = Request.Form.Get("phoneNumber");
            String fileName = Request.Form.Get("fileName");


            if (fileName == null || fileName.Trim().Length == 0 || fileName.IndexOf(".pdf") == -1)
            {
                ViewBag.descErrorSignMobileCA = "File Name is empty";
                return View();
            }

            string pattern = @"[^-_.A-Za-z0-9]";
            Match m = Regex.Match(fileName, pattern, RegexOptions.IgnoreCase);
            if (m.Success)
            {
                ViewBag.descErrorSignMobileCA = "FileName chứa ký tự đặc biệt";
                return View();
            }

            //Get the servers upload directory real path name
            String folderPath = Server.MapPath("~/upload/");
            String fileFullPath = folderPath + "/" + fileName;
            //create the upload folder if not exists
            //logger.info("fileFullPath: " + fileFullPath);
            bool exists = System.IO.File.Exists(fileFullPath);
            if (!exists)
            {
                ViewBag.descErrorSignMobileCA = "File không tồn tại";
                return View();
            }


            string[] stringSeparators = new string[] { ".pdf" };
            String[] chainBase64 = fileName.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
            String name = fileName.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)[0] + "_signed";
            String ext = "pdf";
            String filePath = Server.MapPath("~/upload");

            exists = System.IO.File.Exists(filePath + "/" + name + "." + ext);
            if (exists)
            {
                int index = 1;
                String name_2 = name + "_" + index;
                String path = filePath + "/" + name_2 + "." + ext;
                while (System.IO.File.Exists(path))
                {
                    index++;
                    name_2 = name + "_" + index;
                    path = filePath + "/" + name_2 + "." + ext;
                }
                name = name_2;
            }
            string signedFile = filePath + "/" + name + "." + ext;

            // Contructor
            PdfSignerSynchronous signer = new PdfSignerSynchronous();
            //System.Security.Cryptography.X509Certificates.X509Certificate[] chain = UtilSigner.GetChainCertFromP12(textBoxFileP12.Text, textBoxPasswordFileP12.Text);

            MobileCA mobileCA = new MobileCA();

            string msisdn = phoneNumber;
            bool userSelectCert = true;
            string certSerial = null;
            if (!userSelectCert)
            {
                certSerial = "08276d4f2979228e";
            }

            string dataDisplay = "Thu nghiem ky Mobile-CA";


            //X509Certificate[] chain = mobileCA.GetCertificate(apID, msisdn, certSerial, fileP12, passFileP12);

            String[] certList = mobileCA.GetCertificatefromPrikey(apID, msisdn, certSerial, priKey);

            var certParser = new Org.BouncyCastle.X509.X509CertificateParser();
            Org.BouncyCastle.X509.X509Certificate x509Cert = certParser.ReadCertificate(Convert.FromBase64String(certList[0]));

            Org.BouncyCastle.X509.X509Certificate[] certChain = null;
            X509Certificate certViettelCA = CertUtils.GetX509Cert(certViettelCABase64);
            if (certViettelCA != null)
            {
                certChain = new X509Certificate[] { x509Cert, certViettelCA };
            }

            if (certChain.Length != 2)
            {
                ViewBag.descErrorSignMobileCA = "Lấy Chứng thư số không thành công. Không lấy được CTS CA.";
                return View();
            }

            // Set parameters
            signer.SigTextFormat = PdfSignerSynchronous.FORMAT_TEXT_4;
            signer.SigContact = UtilSigner.GetCNFromDN(x509Cert.SubjectDN.ToString());
            signer.SigLocation = "Hanoi";
            signer.IsMultiSignatures = true;
            signer.Visible = true;
            signer.OriginX = 10;
            signer.OriginY = 10;
            signer.CoordinateX = 300;
            signer.CoordinateY = 50;
            signer.TsaClient = null;
            signer.UseTSA = false;
            DateTime signDate = DateTime.Now;

            // Create hash file

            //Khai bao duong dan toi file pdf can ky tren web server
            //string fileFullPath = @"D:\backup\Project\Viettel CA\Ho Tro\Dot Net\Demo Mobile CA ASP DotNet\PDF\Sample.pdf";
            //ext = DateTime.Now.Ticks.ToString();
            //string signedFile = @"D:\backup\Project\Viettel CA\Ho Tro\Dot Net\Demo Mobile CA ASP DotNet\PDF\Sample_" + ext + ".pdf";
            //byte[] hash = signer.CreateHash(fileFullPath, signedFile, certChain, signDate);
            string base64Hash = HashFilePDF.GetHashTypeRectangleText(fileFullPath, certChain);
            byte[] hash =  Convert.FromBase64String(base64Hash);
            // Sign hash use Prikey

            string signature = mobileCA.signSynchronouswithPrikey(base64Hash, apID, msisdn, dataDisplay, priKey);
            //byte[] signature = mobileCA.signSynchronouswithPrikey(base64Hash, apID, msisdn, dataDisplay, priKey);
            //byte[] signature = mobileCA.signSynchronouswithPrikey(hash, apID, msisdn, dataDisplay, priKey);

            if (signature == null)
            {
                ViewBag.descErrorSignMobileCA = "Phát sinh lỗi trong quá trình thực hiện chữ ký số";
                return View();
            }

            var session = System.Web.HttpContext.Current.Session;

            if (session["pdfSig"] != null)
            {
                try
                {
                    pdfSig = session["pdfSig"] as SignPdfFile;
                }
                catch (Exception e)
                {
                    ViewBag.descErrorSignMobileCA = "Không tìm thấy phiên làm việc";
                    return View();
                }

            }
            if (pdfSig == null)
            {
                ViewBag.descErrorSignMobileCA = "Không tìm thấy phiên làm việc";
                return View();
            }
            TimestampConfig timestampConfig = new TimestampConfig();
            timestampConfig.UseTimestamp = false;
            //string signatureBase64 = Convert.ToBase64String(signature);
            if (!pdfSig.insertSignature(signature, signedFile, timestampConfig))
            {
                ViewBag.descErrorSignMobileCA = "Insert signature into file fail.";
                return View();
            }
            else
            {
                ViewBag.statusResultSignMobileCA = "success";
                ViewBag.signedFileName = name + "." + ext;
            }

            //// Insert signature into file
            //if (signer.InsertSignature(signature))
            //{
            //    ViewBag.statusResultSignMobileCA = "success";
            //    ViewBag.signedFileName = name + "." + ext;
            //}
            //else
            //{
            //    ViewBag.descErrorSignMobileCA = "Phát sinh lỗi khi chèn chữ ký số vào file văn bản";
            //    return View();
            //}

            return View();
        }
    }
}