using dodohold.core;

namespace molilian.core
{

    public class ReplacementRule
    {
        public class Rule
        {
            public class Condition
            {
                public string Value { get; set; }
                public string Operator { get; set; } = "equals";

                public bool Evaluate(string input)
                {
                    return Operator switch
                    {
                        "equals" => input == Value,
                        "contains" => input.Contains(Value),
                        "startsWith" => input.StartsWith(Value),
                        "endsWith" => input.EndsWith(Value),
                        _ => false
                    };
                }
            }
            public Condition When { get; set; }
            public string Then { get; set; }
        }
        public string Name { get; set; }
        public List<Rule> Rules { get; set; }
        public string Default { get; set; }

        internal object FirstOrDefault(Func<object, bool> value)
        {
            throw new NotImplementedException();
        }
    }




    public enum UrlAction
    {
        None = 0,
        Redirect = 1,
        Body = 2,
        JsonBody = 3,
    }

    public class DeeplinkParseRule
    {
        public UrlAction url_action { get; set; } = UrlAction.None;
        /// <summary>
        /// 如果url_atciton = 3 ，用来提取body中的json数据，确保输出的是json格式
        /// </summary>
        public string slice_pattern { get; set; } = string.Empty;
        public string body_pattern { get; set; } = string.Empty;
        public string url_pattern { get; set; } = string.Empty;
        public string pattern { get; set; } = string.Empty;
        public string post { get; set; } = string.Empty;
        public string plugin { get; set; } = string.Empty;
        public List<string[]> replace { get; set; } = new();
        public string deeplink_template { get; set; } = string.Empty;
        public string prompt_text { get; set; } = string.Empty;
        public bool enable { get; set; } = true;
        public List<DeeplinkParseRule> childs { get; set; } = new();
        public List<DeeplinkResources> resources { get; set; } = new();
        public List<ReplacementRule> replacements { get; set; } = new();
    }

    /// <summary>
    /// "resources": [
	///	{
	///		"name": "info",
	///		"url_pattern": "https://api.my.tv.sohu.com/forward/api/h5/getForwardInfo?callback=jsonpx1730973492613_41_1&forward_id={1}",
	///		"url_action": 2,
	///		"pattern": [
    ///           "{\"commentDigg\":{\"id\":\"(\\d+)\"",
    ///           "\"comment_id\":\"(\\d+)\""
    ///        ]
    /// }
	/// ] 
    /// </summary>
    public class DeeplinkResources
    {
        public string name { get; set; } = string.Empty;
        public string post { get; set; } = string.Empty;
        public string url_pattern { get; set; } = string.Empty;
        public UrlAction url_action { get; set; } = UrlAction.None;
        public List<string> pattern { get; set; } = [];
        public bool enable { get; set; } = true;
    }


    [Table("deeplink_parse_rule")]
    public class DeeplinkParseRuleDTO
    {
        [Key]
        public int id { get; set; }
        public int channel_id { get; set; } = 0;
        public int push_channel_id { get; set; } = 0;
        public string channel_name { get; set; } = string.Empty;
        public string name { get; set; } = string.Empty;
        public string logo { get; set; } = string.Empty;
        public string rules { get; set; } = string.Empty;
        public string IgnorePercentageCity { get; set; } = string.Empty;
        public bool status { get; set; } = false;
        public DateTime create_time { get; set; } = DateTime.Now;
        [IgnoreUpdate]
        public DateTime last_time { get; set; } = DateTime.Now;
    }

}
