package com.alibaba.csp.sentinel.dashboard.rule.nacos.processor.impl;

import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.processor.BaseProcessor;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.processor.NacosPostProcessor;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.util.NacosUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Clay
 * @date 2022/11/14
 */
@Service
public class SystemNacosPostProcessor extends BaseProcessor implements NacosPostProcessor {

    @Autowired
    private RuleRepository<SystemRuleEntity, Long> repository;

    @Autowired
    @Qualifier("systemRuleNacosProvider")
    private DynamicRuleProvider<List<SystemRuleEntity>> ruleProvider;

    @Autowired
    @Qualifier("systemRuleNacosPublisher")
    private DynamicRulePublisher<List<SystemRuleEntity>> rulePublisher;


    @Override
    public void initPostProcessor(String app, String ip, Integer port) throws Exception {
        List<SystemRuleEntity> rules = ruleProvider.getRules(app);
        rules = NacosUtils.filterSelf(app,ip,port,rules);
        rules = repository.saveAll(rules);
        sentinelApiClient.setInitRulesAsync(app, ip, port, SentinelApiClient.SYSTEM_RULE_TYPE,rules);
    }

    @Override
    public void initClusterProcessor(String app, String ip, Integer port) throws Exception {
        List<SystemRuleEntity> rules = ruleProvider.getRules(app);
        rules = repository.saveAll(rules);
        sentinelApiClient.setInitRulesAsync(app, ip, port, SentinelApiClient.SYSTEM_RULE_TYPE,rules);
    }

    @Override
    public boolean publishRules(String app, String ip, Integer port, Class<?> clazz) throws Exception {
        if (clazz != SystemRuleEntity.class){
            return false;
        }
        List<SystemRuleEntity> rules = ruleProvider.getRules(app);
        rules = NacosUtils.filterOther(app, ip, port, rules);
        List<SystemRuleEntity> newRules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
        rules.addAll(newRules);
        rulePublisher.publish(app, rules);
        return sentinelApiClient.setSystemRuleOfMachine(app, ip, port, rules);
    }

    @Override
    public boolean publishClusterRules(String app, String ip, Integer port, Class<?> clazz) throws Exception {
        if (clazz != SystemRuleEntity.class){
            return false;
        }
        List<SystemRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
        getAppInfo(app).forEach(info-> sentinelApiClient.setSystemRuleOfMachine(info.getApp(), info.getIp(), info.getPort(), rules));
        rulePublisher.publish(app, rules);
        return true;
    }

    @Override
    public <T> List<T> getRules(String app, String ip, Integer port, Class<?> clazz) throws Exception {
        if (clazz != SystemRuleEntity.class){
            return null;
        }
        List<SystemRuleEntity> rules = ruleProvider.getRules(app);
        return (List<T>) NacosUtils.filterSelf(app, ip, port, rules);
    }

    @Override
    public <T> List<T> getClusterRules(String app, String ip, Integer port, Class<?> clazz) throws Exception {
        if (clazz != SystemRuleEntity.class){
            return null;
        }
        return (List<T>) ruleProvider.getRules(app);
    }
}
