using OASystem.Domain.Dtos.Tencent;
using TencentCloud.Common;
using TencentCloud.Ocr.V20181119.Models;
using TencentCloud.Ocr.V20181119;

namespace OASystem.API.OAMethodLib.TencentCloudAPI
{
    /// <summary>
    /// 腾讯OCR Tools
    /// </summary>
    public static class TencentOCRTools
    {
        /// <summary>
        /// 初始化 腾讯云账户密钥对 SecretId，SecretKey
        /// </summary>
        static Credential cred = new Credential
        {
            SecretId = "AKIDoQUHLBELagatzoScfdMpMddMnz8apqIc",
            SecretKey = "hKiGbhkX5NWWXu3zDWwAATVK3e5sWuBB"
        };

        /// <summary>
        /// ocr识别
        /// </summary>
        /// <param name="ocrEnum">转换类型</param>
        /// <param name="data">请求数据源</param>
        /// <returns></returns>
        public static Result GetOCR(int ocrEnum, object data)
        {
            Result result = new Result { Code = -1, Msg = "未知错误", Data = null };
            OcrClient client = new OcrClient(cred, "ap-guangzhou");

            try
            {
                switch (ocrEnum)
                {
                    case 0: //身份证识别
                        IDCardOCRDto iDCardDto = (IDCardOCRDto)data;
                        IDCardOCRRequest req = new IDCardOCRRequest();

                        if (iDCardDto.CardSide == 0) req.CardSide = "FRONT";
                        else if (iDCardDto.CardSide == 1) req.CardSide = "BACK";

                        req.ImageBase64 = iDCardDto.picBase64;
                        IDCardOCRResponse resp = client.IDCardOCRSync(req);
                        result.Data = resp;
                        break;
                    case 1: //户口本识别
                        ResidenceBookletOCRDto rbOCR = (ResidenceBookletOCRDto)data;
                        ResidenceBookletOCRRequest rbReq = new ResidenceBookletOCRRequest();
                        rbReq.ImageBase64 = rbOCR.picBase64;
                        ResidenceBookletOCRResponse rbRep = client.ResidenceBookletOCRSync(rbReq);
                        result.Data = rbRep;
                        break;
                    case 2: //营业执照识别 OrgCodeCert

                        BizLicenseOCRDto blOCR = (BizLicenseOCRDto)data;
                        BizLicenseOCRRequest blReq = new BizLicenseOCRRequest();
                        blReq.ImageBase64 = blOCR.picBase64;
                        BizLicenseOCRResponse blRep = client.BizLicenseOCRSync(blReq);
                        result.Data = blRep;
                        break;
                    case 3: //组织机构代码证识别 

                        OrgCodeCertOCRDto occOCR = (OrgCodeCertOCRDto)data;
                        OrgCodeCertOCRRequest ocReq = new OrgCodeCertOCRRequest();
                        ocReq.ImageBase64 = occOCR.picBase64;
                        OrgCodeCertOCRResponse occReq = client.OrgCodeCertOCRSync(ocReq);
                        result.Data = occReq;
                        break;
                    case 4: //行驶证识别 

                        VehicleLicenseOCRDto vlOCR = (VehicleLicenseOCRDto)data;
                        VehicleLicenseOCRRequest vlReq = new VehicleLicenseOCRRequest();

                        if (vlOCR.CardSide == 0) vlReq.CardSide = "FRONT";
                        else if (vlOCR.CardSide == 1) vlReq.CardSide = "BACK";
                        else if (vlOCR.CardSide == 2) vlReq.CardSide = "DOUBLE";

                        vlReq.ImageBase64 = vlOCR.picBase64;
                        VehicleLicenseOCRResponse vlRep = client.VehicleLicenseOCRSync(vlReq);
                        result.Data = vlRep;
                        break;
                    case 5: //房产证识别 通用文字识别
                        PropOwnerCertOCRDto pocOCR = (PropOwnerCertOCRDto)data;
                        PropOwnerCertOCRRequest pocReq = new PropOwnerCertOCRRequest();
                        pocReq.ImageBase64 = pocOCR.picBase64;
                        PropOwnerCertOCRResponse pocRep = client.PropOwnerCertOCRSync(pocReq);
                        result.Data = pocRep;
                        break;
                    case 6: // 通用文字识别
                        GeneralBasicOCRDto gbOCR = (GeneralBasicOCRDto)data;
                        GeneralBasicOCRRequest gbReq = new GeneralBasicOCRRequest();
                        gbReq.ImageBase64 = gbOCR.picBase64;
                        GeneralBasicOCRResponse gbRep = client.GeneralBasicOCRSync(gbReq);
                        result.Data = gbRep;
                        break;
                    case 7: // 护照识别（中国大陆地区护照）
                        PassportOCRDto pOCR = (PassportOCRDto)data;
                        PassportOCRRequest pReq = new PassportOCRRequest();
                        pReq.ImageBase64 = pOCR.picBase64;
                        PassportOCRResponse pRep = client.PassportOCRSync(pReq);
                        result.Data = pRep;
                        break;
                    case 8: // 护照识别（港澳台地区及境外护照）
                        MLIDPassportOCRDto mlidpOCR = (MLIDPassportOCRDto)data;
                        MLIDPassportOCRRequest mlidpReq = new MLIDPassportOCRRequest();
                        mlidpReq.ImageBase64 = mlidpOCR.picBase64;
                        MLIDPassportOCRResponse mlidpRep = client.MLIDPassportOCRSync(mlidpReq);
                        result.Data = mlidpRep;
                        break;
                    case 9: // 护照识别（港澳台地区及境外护照）
                        BusinessCardOCRDto bcOCR = (BusinessCardOCRDto)data;
                        BusinessCardOCRRequest bcReq = new BusinessCardOCRRequest();
                        bcReq.ImageBase64 = bcOCR.picBase64;
                        BusinessCardOCRResponse bcRep = client.BusinessCardOCRSync(bcReq);
                        result.Data = bcRep;
                        break;
                }

                result.Code = 0;
                result.Msg = "成功";
                return result;
            }
            catch (Exception ex)
            {
                result.Msg = "TencentOCRAPIError：" + ex.Message;
                return result;
            }
        }


        /// <summary>
        /// 图片转Base64
        /// </summary>
        /// <param name="ImageFileName">图片的完整路径</param>
        /// <returns></returns>
        public static string ImgToBase64(string ImageFileName)
        {
            try
            {
                Bitmap bmp = new Bitmap(ImageFileName);
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                return Convert.ToBase64String(arr);
            }
            catch (Exception e)
            {
                return e.Message.ToString();
            }
        }

        /// <summary>
        /// TencentOCR图片类型处理
        /// PNG、JPG、JPEG、BMP
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool ImageType(string type)
        {
            if (type.ToLower() == "png" || type.ToLower() == "jpg" || type.ToLower() == "jpeg" || type.ToLower() == "bmp")
                return true;

            return false;
        }


        private const double KBCount = 1024;
        private const double MBCount = KBCount * 1024;
        private const double GBCount = MBCount * 1024;
        private const double TBCount = GBCount * 1024;

        /// <summary>
        /// 得到适应的大小
        /// </summary>
        /// <param name="path"></param>
        /// <returns>string</returns>
        public static string GetAutoSizeString(double size, int roundCount)
        {
            if (KBCount > size)
            {
                return Math.Round(size, roundCount) + "B";
            }
            else if (MBCount > size)
            {
                return Math.Round(size / KBCount, roundCount) + "KB";
            }
            else if (GBCount > size)
            {
                return Math.Round(size / MBCount, roundCount) + "MB";
            }
            else if (TBCount > size)
            {
                return Math.Round(size / GBCount, roundCount) + "GB";
            }
            else
            {
                return Math.Round(size / TBCount, roundCount) + "TB";
            }
        }
    }
}
