package com.narutohuo.xindazhou.push.helper

import android.content.Context
import cn.jpush.android.api.JPushMessage
import com.narutohuo.xindazhou.core.log.ILog

/**
 * 标签和别名操作辅助类
 * 
 * 用于处理极光推送的标签和别名操作结果
 * 参考极光推送官方示例
 */
class TagAliasOperatorHelper private constructor() {
    
    companion object {
        private const val TAG = "TagAliasHelper"
        
        @Volatile
        private var instance: TagAliasOperatorHelper? = null
        
        @JvmStatic
        fun getInstance(): TagAliasOperatorHelper {
            return instance ?: synchronized(this) {
                instance ?: TagAliasOperatorHelper().also { instance = it }
            }
        }
    }
    
    private var context: Context? = null
    
    /**
     * 初始化（可选）
     */
    fun init(context: Context) {
        this.context = context.applicationContext
    }
    
    /**
     * 处理标签操作结果
     */
    fun onTagOperatorResult(context: Context, jPushMessage: JPushMessage) {
        val sequence = jPushMessage.sequence
        ILog.d(TAG, "onTagOperatorResult, sequence=$sequence, tags=${jPushMessage.tags}")
        ILog.d(TAG, "tags size=${jPushMessage.tags?.size ?: 0}")
        
        init(context)
        
        if (jPushMessage.errorCode == 0) {
            ILog.d(TAG, "标签操作成功, sequence=$sequence")
        } else {
            var logs = "标签操作失败"
            if (jPushMessage.errorCode == 6018) {
                // tag数量超过限制，需要先清除一部分再add
                logs += ", 标签数量超过限制，需要先清除一部分"
            }
            logs += ", errorCode=${jPushMessage.errorCode}"
            ILog.e(TAG, logs)
        }
    }
    
    /**
     * 处理检查标签操作结果
     */
    fun onCheckTagOperatorResult(context: Context, jPushMessage: JPushMessage) {
        val sequence = jPushMessage.sequence
        ILog.d(TAG, "onCheckTagOperatorResult, sequence=$sequence, checkTag=${jPushMessage.checkTag}")
        
        init(context)
        
        if (jPushMessage.errorCode == 0) {
            val logs = "检查标签 ${jPushMessage.checkTag} 绑定状态成功, state=${jPushMessage.tagCheckStateResult}"
            ILog.d(TAG, logs)
        } else {
            val logs = "检查标签失败, errorCode=${jPushMessage.errorCode}"
            ILog.e(TAG, logs)
        }
    }
    
    /**
     * 处理别名操作结果
     */
    fun onAliasOperatorResult(context: Context, jPushMessage: JPushMessage) {
        val sequence = jPushMessage.sequence
        ILog.d(TAG, "onAliasOperatorResult, sequence=$sequence, alias=${jPushMessage.alias}")
        
        init(context)
        
        if (jPushMessage.errorCode == 0) {
            ILog.d(TAG, "别名操作成功, sequence=$sequence")
        } else {
            val logs = "别名操作失败, errorCode=${jPushMessage.errorCode}"
            ILog.e(TAG, logs)
        }
    }
    
    /**
     * 处理手机号操作结果
     */
    fun onMobileNumberOperatorResult(context: Context, jPushMessage: JPushMessage) {
        val sequence = jPushMessage.sequence
        ILog.d(TAG, "onMobileNumberOperatorResult, sequence=$sequence, mobileNumber=${jPushMessage.mobileNumber}")
        
        init(context)
        
        if (jPushMessage.errorCode == 0) {
            ILog.d(TAG, "手机号操作成功, sequence=$sequence")
        } else {
            val logs = "手机号操作失败, errorCode=${jPushMessage.errorCode}"
            ILog.e(TAG, logs)
        }
    }
    
    /**
     * 验证标签和别名格式是否正确
     * 只能包含：数字、英文字母、中文、以及部分特殊字符
     */
    fun isValidTagAndAlias(s: String): Boolean {
        if (s.isEmpty()) return false
        
        val pattern = Regex("^[\u4E00-\u9FA50-9a-zA-Z_!@#$&*+=.|]+$")
        return pattern.matches(s)
    }
    
    /**
     * 验证手机号格式
     * 只能以 "+" 或者数字开头；后面的内容只能包含 "-" 和数字
     */
    fun isValidMobileNumber(s: String): Boolean {
        if (s.isEmpty()) return true
        
        val pattern = Regex("^[+0-9][-0-9]{1,}$")
        return pattern.matches(s)
    }
}

