package quant.rich.emoney.controller.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import quant.rich.emoney.controller.common.UpdateBoolServiceController;
import quant.rich.emoney.entity.sqlite.ProxySetting;
import quant.rich.emoney.exception.RException;
import quant.rich.emoney.pojo.dto.LayPageReq;
import quant.rich.emoney.pojo.dto.LayPageResp;
import quant.rich.emoney.pojo.dto.R;
import quant.rich.emoney.service.sqlite.ProxySettingService;

@Slf4j
@Controller
@RequestMapping("/admin/v1/manage/proxySetting")
public class ProxySettingControllerV1 extends UpdateBoolServiceController<ProxySetting> {
    
    @Autowired
    ProxySettingService proxySettingService;
    
    @GetMapping({"", "/", "/index"})
    public String index() {
        return "/admin/v1/manage/proxySetting/index";
    }
	
	@GetMapping("/list")
	@ResponseBody
	public LayPageResp<?> list(LayPageReq<ProxySetting> pageReq) {
	    return super.list(pageReq);
	}
	
	@GetMapping("/getOne")
	@ResponseBody
	public R<?> getOne(String id) {
	    return super.getOne(id);
	}
	
	@PostMapping("/save")
	@ResponseBody
	public R<?> save(@RequestBody ProxySetting proxySetting) {
	    R<?> result = super.save(proxySetting);
	    proxySettingService.clearIpInfoCache();
	    return result;
	}

	@PostMapping("/delete")
	@ResponseBody
	public R<?> delete(String id) {
	    R<?> result = super.delete(id);
	    proxySettingService.clearIpInfoCache();
	    return result;
	}

    @Override
    protected
    R<?> updateBool(String id, String field, Boolean value) {
        R<?> result = super.updateBool(id, field, value);
        if ("isDefault".equals(field)) {
            proxySettingService.clearIpInfoCache();
        }
        return result;
    }
	
	@PostMapping("/batchOp")
    @ResponseBody
    public R<?> batchOp(
            @RequestParam(value="ids[]", required=true)
            String[] ids, ProxySettingBatchOp op) {
        if (Objects.isNull(ids) || ids.length == 0) {
            throw RException.badRequest("提供的计划 ID 不能为空");
        }
        List<String> idArray = Arrays.asList(ids);
        
        if (op == null) {
            // op 为空是删除
        	throw RException.badRequest("操作类型不能为空");
        }
        else if (ProxySettingBatchOp.DELETE == op) {
            R<?> result = R.judge(
                    proxySettingService.removeBatchByIds(idArray));
            proxySettingService.clearIpInfoCache();
            return result;
        }
        
        LambdaUpdateWrapper<ProxySetting> uw = new LambdaUpdateWrapper<>();
        uw.in(ProxySetting::getId, idArray);
        
        switch (op) {
        case CHECK:
        	// TODO: 检查连通性
        	break;
        case DISABLE_HTTPS_VERIFY:
            uw.set(ProxySetting::getIgnoreHttpsVerification, false);
            break;
        case ENABLE_HTTP_VERIFY:
            uw.set(ProxySetting::getIgnoreHttpsVerification, true);
            break;
    	default:
        	throw RException.badRequest("未知操作");
        }
        return R.judge(() -> proxySettingService.update(uw));
    }
	
	private static enum ProxySettingBatchOp {
		DELETE,
		CHECK,
		DISABLE_HTTPS_VERIFY,
		ENABLE_HTTP_VERIFY
	}
	
	
    @GetMapping("/refreshIpThroughProxy")
    @ResponseBody
    public R<?> refreshIpThroughProxy() {
        return R.ok(
                proxySettingService
                .refreshIpThroughProxy());
    }

}
