using System;
using System.Collections.Generic;
using System.Text;

namespace ryPrint.Mod
{
    /// <summary>
    /// 智能分析地址
    /// </summary>
    public class SmartAnalyAddr
    {
        public static string GetStr(string input, string point1, string point2, int iPos1, out int endPos, string defValue)
        {
            int iIndex = iPos1;
            endPos = -1;
            if (iPos1 < 0) { return defValue; }
            if (point1 != "")
            { iIndex = input.IndexOf(point1, iPos1); }
            if (iIndex == -1) { return defValue; }
            int iIndex2 = 0;
            if (point2 == "") { iIndex2 = input.Length; }
            else { iIndex2 = input.IndexOf(point2, iIndex + point1.Length); }
            string tmpStr = input;
            if (iIndex < iIndex2 && iIndex >= 0)
            {
                tmpStr = input.Substring(iIndex + point1.Length, iIndex2 - iIndex - point1.Length);
                endPos = iIndex2 + point2.Length;
            }
            else
            {
                return defValue;
            }
            return tmpStr;
        }
        public static AddrType AnalyAddr(string addr)
        {
            AddrType result = new AddrType();
            int iPos = 0;
            int validPos = 0;
            result.Province = GetStr(addr, "", "省", 0, out iPos, "").Trim();
            if (iPos >= 0) { validPos = iPos; }
            result.City = GetStr(addr, "", "市", validPos, out iPos, "").Trim(); if (iPos >= 0) { validPos = iPos; }
            result.County = GetStr(addr, "", "县", validPos, out iPos, "").Trim(); if (iPos >= 0) { validPos = iPos; }
            if (result.County != "") { result.County += "县"; }
            if (result.County=="")
            {
                result.County = GetStr(addr, "", "市", validPos, out iPos, "").Trim(); if (iPos >= 0) { validPos = iPos; }
                if (result.County != "") { result.County += "市"; }
            }
            if (result.County == "")
            {
                result.County = GetStr(addr, "", "区", validPos, out iPos, "").Trim(); if (iPos >= 0) { validPos = iPos; }
                if (result.County != "") { result.County += "区"; }
            }
            if(result.County=="")
            {
                if (addr.IndexOf("瑞安市") > 0) { result.City = "温州"; result.County = "瑞安市"; }
            }
            result.AddrDetail = addr.Substring(validPos);
            return result;
        }
        public static Tb_AddrType AnalyTaobaoAddr(string addr)
        {
            Tb_AddrType result = new Tb_AddrType();
            int iPos = 0;
            int validPos = 0;
            result.Province = GetStr(addr, "", " ", 0, out iPos, "");
            if (iPos >= 0) { validPos = iPos; }
            result.City = GetStr(addr, "", " ", validPos, out iPos, ""); if (iPos >= 0) { validPos = iPos; }
            result.County = GetStr(addr, "", " ", validPos, out iPos, ""); if (iPos >= 0) { validPos = iPos; }
            int r_k = addr.LastIndexOf(")");
            int l_k = addr.LastIndexOf("(");
            if (r_k > l_k)
            {
                result.PCode = addr.Substring(l_k + 1, r_k - l_k-1);
                result.Addr = addr.Substring(0, l_k).Trim('，');
            }
            else
            { result.Addr = addr.Trim('，'); }
            if (l_k > validPos)
            {
                result.AddrDetail = addr.Substring(validPos, l_k- validPos).Trim('，');
            }
            else
            { result.AddrDetail = addr.Substring(validPos).Trim('，'); }
            return result;
        }
    }
    public class AddrType
    {
        /// <summary>
        /// 省份
        /// </summary>
       public string Province { get; set; } = "";
        /// <summary>
        /// 所在市
        /// </summary>
        public string City { get; set; } = "";
        /// <summary>
        /// 所在县区
        /// </summary>
        public string County { get; set; } = "";
        /// <summary>
        /// 详细地址
        /// </summary>
        public string AddrDetail { get; set; } = "";
    }
    public class Tb_AddrType
    {
        /// <summary>
        /// 省份
        /// </summary>
        public string Province { get; set; } = "";
        /// <summary>
        /// 所在市
        /// </summary>
        public string City { get; set; } = "";
        /// <summary>
        /// 所在县区
        /// </summary>
        public string County { get; set; } = "";
        /// <summary>
        /// 详细地址
        /// </summary>
        public string AddrDetail { get; set; } = "";
        /// <summary>
        /// 
        /// </summary>
        public string Addr { get; set; } = "";
        /// <summary>
        /// 邮编
        /// </summary>
        public string PCode { get; set; } = "";
    }
}
