package com.bcrjl.miwifi.controller;

import com.bcrjl.miwifi.common.response.R;
import com.bcrjl.miwifi.common.util.MiWifiUtil;
import com.bcrjl.miwifi.model.param.DeleteRedirectParam;
import com.bcrjl.miwifi.model.param.RangeRedirectParam;
import com.bcrjl.miwifi.model.param.RedirectParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author yanqs
 */
@AllArgsConstructor
@RestController
@RequestMapping("/api/network")
public class NetworkController {

    /**
     * 网络工具类
     */
    private final MiWifiUtil miWifiUtil;

    /**
     * 获取外网状态
     *
     * @return
     */
    @GetMapping("pppoe-status")
    public R pppoeStatus() {
        return R.data(miWifiUtil.getPppoeStatus());
    }


    /**
     * 端口转发
     *
     * @param type 类型
     * @return 列表
     */
    @GetMapping("/port-forward")
    public R portForward(Integer type) {
        return R.data(miWifiUtil.portForward(type));
    }

    /**
     * 添加端口转发
     *
     * @param param 请求参数
     * @return R
     */
    @PostMapping("/add-redirect")
    public R addRedirect(@RequestBody RedirectParam param) {
        Map<String, Object> result = miWifiUtil.addRedirect(param);
        if (Boolean.parseBoolean(result.get("status").toString())) {
            return R.success();
        }
        return R.fail(result.get("msg").toString());
    }

    /**
     * 添加端口(范围)转发
     *
     * @param param 请求参数
     * @return R
     */
    @PostMapping("/add-range-redirect")
    public R addRangeRedirect(@RequestBody RangeRedirectParam param) {
        Map<String, Object> result = miWifiUtil.addRangeRedirect(param);
        if (Boolean.parseBoolean(result.get("status").toString())) {
            return R.success();
        }
        return R.fail(result.get("msg").toString());
    }

    /**
     * 删除端口转发
     *
     * @param param 请求参数
     * @return R
     */
    @PostMapping("/delete-redirect")
    public R deleteRedirect(@RequestBody DeleteRedirectParam param) {
        Map<String, Object> result = miWifiUtil.deleteRedirect(param);
        if (Boolean.parseBoolean(result.get("status").toString())) {
            return R.success();
        }
        return R.fail(result.get("msg").toString());
    }
}
