using dodohold.core;
using System.Text.RegularExpressions;

namespace molilian.core
{
    public partial class PddUnionPlus
    {
        public static string _url_pattern = @"https?://(?:[\w-]+\.)*(?:yangkeduo\.com|pinduoduo\.com)(?:/[^\s]*)?";
        public static string GetLink(string content)
        {
            if (string.IsNullOrEmpty(content)) return content;
            string result = content;
            //
            Regex regex = new(_url_pattern);
            MatchCollection matches = regex.Matches(content);

            foreach (Match m in matches.Cast<Match>())
            {
                string url = m.Value;
                return url;
            }
            return string.Empty;
        }


        public static string GetDeeplink(string url)
        {
            string result = $"pinduoduo://com.xunmeng.pinduoduo/{url}";
            return result;
        }

        public static PddDataDTO GetFormattedObject(string content, string ip = "", string oaid = "")
        {
            string shortLinkurl = GetLink(content);
            string deeplink_url = GetDeeplink(shortLinkurl);
            return new PddDataDTO()
            {
                message = string.Empty,
                link_type = LinkTypeEnum.unknown,
                channel = TkChannelEnum.pdd,
                accountName = string.Empty,
                success = false,
                content = content,
                ip = ip,
                oaid = oaid,
                elapsedTime = 0,
                itemName = "点击打开拼多多APP",
                shortLinkurl = string.Empty,
                deeplink_url = string.Empty,
                create_time = DateTime.Now,
                end_point = _end_point,
            };
        }

        public async Task<PddDataDTO> PddParseAsync(string content, PddDataDTO result, CancellationToken cancellationToken = default)
        {
            string message = "OK";
            string url = GetLink(content);
            if (string.IsNullOrEmpty(url))
            {
                result.success = false;
                result.link_type = LinkTypeEnum.unknown;
                result.channel_type = ChannelTypeEnum.pdd;
                result.message = "放弃转链";
                result.reason = "无效链接";
                result.accountId = 0;
                result.accountName = string.Empty;
                result.content = content;
                return result;
            }
            string shortLinkurl = url;

            if (result.link_type == LinkTypeEnum.other_aff)
            {
                result.success = false;
                result.link_type = LinkTypeEnum.unknown;
                result.channel_type = ChannelTypeEnum.pdd;
                result.message = "放弃转链";
                result.reason = "其他推广链接";
                result.accountId = 0;
                result.accountName = string.Empty;
                result.content = content;
                return result;
            }

            if (!result.ip.StartsWith("127.0.0") && FlowControlIgnoreRequest(_config, out string reason))
            {
                result.success = false;
                result.link_type = LinkTypeEnum.unknown;
                result.channel_type = ChannelTypeEnum.pdd;
                result.message = "放弃转链";
                result.reason = reason;
                result.accountId = 0;
                result.accountName = string.Empty;
                result.content = content;
                return result;
            }

            switch (_account.work_mode)
            {
                case PddUnionWorkMode.SiteApi:
                    result = await UnionGeneratelink(result, url, cancellationToken);
                    break;
                //case PddUnionWorkMode.AffApi:
                //    result = await GetPromotionByAffAsync(result, url, cancellationToken);
                //    break;
                case PddUnionWorkMode.Crawler:
                default:
                    result = await transferUrl(result, url, cancellationToken);
                    break;
            }
            return result;
        }
    }
}
