package com.ruoyi.cc.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.cc.domain.CcParams;
import com.ruoyi.cc.domain.FirewallRule;
import com.ruoyi.cc.service.IFirewallService;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 *  firewalld Controller
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@Controller
@RequestMapping("/system/firewalld")
public class CcFirewalldController extends BaseController
{
    private String prefix = "system/firewalld";

    @Autowired
    private IFirewallService firewallService;

    @RequiresPermissions("system:firewalld:view")
    @GetMapping()
    public String firewalld()
    {
        return prefix + "/firewalld";
    }

    /**
     * list firewall rules
     */
    @RequiresPermissions("system:firewalld:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list() throws Exception
    {
        startPage();
        List<FirewallRule> list = firewallService.getAllList();
        return getDataTable(list);
    }

    @RequiresPermissions("system:firewalld:export")
    @Log(title = "firewalld rules", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export() throws Exception {
        List<FirewallRule> list = firewallService.getAllList();
        ExcelUtil<FirewallRule> util = new ExcelUtil<FirewallRule>(FirewallRule.class);
        return util.exportExcel(list, "firewalld rules");
    }

    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        FirewallRule rule = new FirewallRule();
        mmap.put("firewallRule", rule);
        return prefix + "/add";
    }

    @RequiresPermissions("system:firewalld:add")
    @Log(title = "firewalld add rules", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FirewallRule rule) throws Exception {
        AjaxResult result = toAjax(firewallService.add(rule));
        String reloadRsp = firewallService.reloadFirewalld();
        result.put("firewalld_response", reloadRsp);
        return result;
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        try {
            List<FirewallRule> list = firewallService.getAllList();
            for (FirewallRule rule: list) {
                if (id.equals(rule.getId())) {
                    mmap.put("firewallRule", rule);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return prefix + "/edit";
    }


    @RequiresPermissions("system:firewalld:edit")
    @Log(title = "firewalld rule edit", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FirewallRule rule) throws Exception {
        AjaxResult result = toAjax(firewallService.update(rule));
        String reloadRsp = firewallService.reloadFirewalld();
        result.put("firewalld_response", reloadRsp);
        return result;
    }

    @RequiresPermissions("system:firewalld:remove")
    @Log(title = "firewalld rules remove", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            if (StringUtils.isNotEmpty(ids)) {
                List<FirewallRule> list = firewallService.getAllList();
                for (String id: ids.split(",")) {
                    logger.info("待删除id:{}", id);
                    for (FirewallRule firewallRule: list) {
                        logger.info("规则:{}", JSONObject.toJSONString(firewallRule));
                        if (id.equals(firewallRule.getId())) {
                            firewallService.delete(firewallRule);
                        }
                    }
                }
            }
            firewallService.reloadFirewalld();
        } catch (Exception e) {
            logger.error(ExceptionUtil.getExceptionMessage(e));
        }
       return toAjax(1);
    }

}
