using Autofac.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OASystem.API.OAMethodLib;
using OASystem.API.OAMethodLib.BaiduApi;
using OASystem.API.OAMethodLib.TencentCloudAPI;
using OASystem.Domain.Dtos.Baidu;
using OASystem.Domain.Dtos.CRM;
using OASystem.Domain.Entities.Customer;
using OASystem.Domain.ViewModels.OCR;
using Quartz.Util;

namespace OASystem.API.Controllers
{

    /// <summary>
    /// 百度OCR识别
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BaiduOCRController : ControllerBase
    {
        /// <summary>
        /// 接团客户OCR识别
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public IActionResult ClientOCR(ClientOCRDto dto)
        {
            try
            {
                string[] picBase64Array = dto.base64img.Split(';');
                string picFormat = picBase64Array[0].Split('/')[1];
                if (!TencentOCRTools.ImageType(picFormat))
                {
                    return Ok(JsonView("图片格式不正确!只支持 PNG、JPG、JPEG、BMP 格式!"));
                }
            }
            catch (Exception ex)
            {
                return Ok(JsonView("请上传图片base64编码数据！"));
            }

            JsonView jw = new JsonView();
            try
            {
                string api_Response = BaiduApiHelper._apiTableV2(dto.base64img);
                JObject json = JObject.Parse(api_Response);
                if (json["tables_result"][0]["body"] != null)
                {

                    List<BaiduOCR_TableV2> sourceList = JsonConvert.DeserializeObject<List<BaiduOCR_TableV2>>(json["tables_result"][0]["body"].ToString());
                    List<BaiduOCR_TableV2> headerList = sourceList.Where(s => s.row_start == 0).OrderBy(s => s.col_start).Select(x => new BaiduOCR_TableV2
                    {
                        col_end = x.col_end,
                        col_start = x.col_start,
                        row_end = x.row_end,
                        row_start = x.row_start,
                        words = Regex.Replace(x.words, "[`~!@#$%^&*()_\\-+=<>?:\"{}|,.\\/;'\\\\[\\]·！#￥（——）：；“”‘、，|《。》？、【】[\\]]", string.Empty)
                    }).ToList();
                    List<BaiduOCR_TableV2> dataList = sourceList.Where(s => s.row_start != 0).ToList();
                    int maxRowIndex = dataList.Max(s => s.row_start);
                    int maxColIndex = headerList.Max(s => s.col_start) + 1;
                    Dictionary<int, string> dicHeader = new Dictionary<int, string>();
                    List<string> listNameStr = new List<string>() { "姓名", "name", "姓 名" };
                    List<string> listSexStr = new List<string>() { "性别", "gender", "sex" };
                    List<string> listDOBStr = new List<string>() { "D.O.B", "出生年月", "生日", "出生日期" };
                    List<string> listIdCard = new List<string>() { "身份证号码", "身份证", "身份证号" };
                    List<string> listOrganizationStr = new List<string>() { "工作单位", "organization", "单位"};
                    List<string> listJobStr = new List<string>() { "职务", "title", "职位", "所在单位及职务" , "单位及职务" };
                    List<string> listMobileStr = new List<string>() { "联系电话", "mobile" };
                    List<string> listAgeStr = new List<string>() { "年龄", "age" };
                    List<string> listBeiZhu = new List<string>() { "备注", "本团职务" };

                    for (int i = 0; i < headerList.Count; i++)
                    {
                        string words = headerList[i].words.Trim().ToLower();
                        if (listNameStr.Exists(s => s == words))
                        {
                            dicHeader.Add(i, "Name");
                        }
                        else if (listSexStr.Exists(s => s == words))
                        {
                            dicHeader.Add(i, "Sex");
                        }
                        else if (listDOBStr.Exists(s => s == words))
                        {
                            dicHeader.Add(i, "DOB");
                        }
                        else if (listIdCard.Exists(s => s == words))
                        {
                            dicHeader.Add(i, "IdCard");
                        }
                        else if (listOrganizationStr.Exists(s => s == words))
                        {
                            dicHeader.Add(i, "Organization");
                        }
                        else if (listJobStr.Exists(s => s == words))
                        {
                            dicHeader.Add(i, "Job");
                        }
                        else if (listMobileStr.Exists(s => s == words))
                        {
                            dicHeader.Add(i, "Mobile");
                        }
                        else if (listAgeStr.Exists(s => s == words))
                        {
                            dicHeader.Add(i, "Age");
                        }
                        else if (listBeiZhu.Exists(s => s == words))
                        {
                            dicHeader.Add(i, "BeiZhu");
                        }
                    }

                    List<BaiduClientOCRView> ClientArr = new List<BaiduClientOCRView>();
                    for (int i = 1; i <= maxRowIndex; i++)
                    {
                        BaiduClientOCRView client = new BaiduClientOCRView();
                        for (int j = 0; j < maxColIndex; j++)
                        {
                            var item = dataList.First(s => s.row_start == i && s.col_start == j);
                            string words = item.words.Replace("\\n", "");
                            string values = string.Empty;
                            try
                            {
                                values = dicHeader[j];
                            }
                            catch (Exception)
                            {
                               throw new Exception("有未能识别的列名请检查 : " + JsonConvert.SerializeObject(headerList.Select(x => x.words)));
                            }

                            if (words.IsNullOrWhiteSpace())
                            {
                                continue;
                            }

                            if (values.Equals("Name"))
                            {
                                if (Regex.Matches(words, "[a-zA-Z]").Count < 1)
                                {
                                    string lastName = words.Substring(0, 1);
                                    string name = words.Substring(1);
                                    client.LastName = lastName;
                                    client.FirstName = name;
                                    string lastNamePinYin = lastName.GetTotalPingYin().Count > 0 ? lastName.GetTotalPingYin()[0].ToUpper():"";
                                    string firstNamePinYin = "";

                                    for (int n = 0; n < name.Length; n++)
                                    {
                                        firstNamePinYin += name[n].ToString().GetTotalPingYin().Count > 0 ? name[n].ToString().GetTotalPingYin()[0].ToUpper() + " " : "";
                                    }

                                    client.Pinyin = lastNamePinYin + "/" + firstNamePinYin;
                                }
                                else
                                {
                                    Regex regForeign = new Regex("[a-zA-Z]+[\\s][a-zA-Z]+");
                                    if (regForeign.IsMatch(words))
                                    {
                                        string[] names = words.Split(' ');
                                        client.LastName = names[1];
                                        client.FirstName = names[0];
                                    }
                                    else
                                    {
                                        client.FirstName = words;
                                    }
                                }
                            }
                            else if (values.Equals("Sex"))
                            {
                                client.Sex = words == "男" ? 0 : 1;
                            }
                            else if (values.Equals("DOB"))
                            {
                                client.Birthday = words.Replace(".", "-");
                                DateTime time = new DateTime();
                                if (DateTime.TryParse(client.Birthday, out time))
                                {
                                    client.Birthday = time.ToString("yyyy-MM-dd");
                                }
                                else
                                {
                                    client.Birthday = "";
                                }

                            }
                            else if (values.Equals("IdCard"))
                            {
                                client.IDcard = words;
                            }
                            else if (values.Equals("Organization"))
                            {
                                client.CompanyFullName = words;
                            }
                            else if (values.Equals("Job"))
                            {
                                client.Job = words;
                            }
                            else if (values.Equals("Mobile"))
                            {
                                client.Phone = words;
                            }
                        }

                        if(!(client.FirstName + client.LastName + client.Pinyin + client.CompanyFullName + client.Job + client.Phone + client.Birthday).IsNullOrWhiteSpace())
                        {
                            if (string.IsNullOrWhiteSpace(client.CompanyFullName))
                            {
                                client.CompanyFullName = "暂无";
                            }
                            ClientArr.Add(client);
                        }
                    }
                    jw.Code = 200;
                    jw.Msg = "获取成功！";
                    jw.Data = ClientArr;

                }
                else if (json["error_code"] != null)
                {
                    jw.Data = new string[0];
                    jw.Msg = "百度接口异常！";
                }
                else
                {
                    jw.Data = new string[0];
                    jw.Msg = "百度接口异常！";
                }
            }
            catch (Exception ex)
            {
                jw.Code = 400;
                jw.Msg = "程序异常！" + ex.Message;
                jw.Data = new string[0];
            }

            return Ok(jw);
        }




    }
}
