package com.ruoyi.cc.service.impl;

import com.ruoyi.cc.domain.FirewallRule;
import com.ruoyi.cc.service.ICcParamsService;
import com.ruoyi.cc.service.IFirewallService;
import com.ruoyi.cc.utils.FirewalldXmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class FirewallServiceImpl implements IFirewallService {

    @Autowired
    private ICcParamsService ccParamsService;

    @Autowired
    private FirewalldXmlUtil firewalldXmlUtil;

    @Override
    public boolean add(FirewallRule rule) throws Exception {
        return  firewalldXmlUtil.addRule(rule);
    }

    @Override
    public List<FirewallRule> getAllList() throws Exception {
        return firewalldXmlUtil.getAllRules();
    }

    @Override
    public boolean delete(FirewallRule rule) throws  Exception {
         return firewalldXmlUtil.delete(rule);
    }

    @Override
    public boolean update(FirewallRule rule) throws Exception {
        List<FirewallRule> list = getAllList();
        for (FirewallRule firewallRule: list) {
            if (firewallRule.getId().equals(rule.getId())) {
                delete(firewallRule);
            }
        }
        return add(rule);
    }

    @Override
    public String reloadFirewalld()  throws Exception  {
        return ccParamsService.reloadParams();
    }
}