package com.judong.chuanyiserver.controller;

import com.judong.chuanyiserver.annotation.PermissionControl;
import com.judong.chuanyiserver.entity.ServerInformation;
import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.service.ConnectService;
import com.judong.chuanyiserver.util.Blank;
import com.judong.chuanyiserver.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.jinterop.dcom.common.JIException;
import org.openscada.opc.lib.common.AlreadyConnectedException;
import org.openscada.opc.lib.common.NotConnectedException;
import org.openscada.opc.lib.da.AddFailedException;
import org.openscada.opc.lib.da.DuplicateGroupException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.UnknownHostException;

@RestController
@RequestMapping("connect")
@Slf4j
public class ConnectController {

    @Autowired
    private ConnectService connectService;

    /**
     * 测试连接是否可用
     *
     * @param serverInformation
     * @return
     * @throws AlreadyConnectedException
     * @throws JIException
     * @throws UnknownHostException
     */
    @GetMapping("/testConnect")
    public Result testConnect(ServerInformation serverInformation) throws AlreadyConnectedException, JIException, UnknownHostException {
        if (Blank.isEmpty(serverInformation, serverInformation.getIpAddress(), serverInformation.getIpUserName(), serverInformation.getIpPassword(), serverInformation.getAgreementType())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return connectService.testConnect(serverInformation);
    }

    /**
     * 保存用户提交的连接信息
     *
     * @param serverInformation
     * @return
     */
    @PostMapping("/saveConnect")
//    @PermissionControl
    public Result saveConnect(ServerInformation serverInformation) {
        if (Blank.isEmpty(serverInformation, serverInformation.getIpAddress(), serverInformation.getIpUserName(), serverInformation.getIpPassword(), serverInformation.getAgreementType())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return connectService.saveConnect(serverInformation);
    }

    /**
     * 查询用户保存的连接信息
     *
     * @return
     */
    @GetMapping("/selectAllConnect")
    public Result selectAllConnect() {
        return connectService.selectAllConnect();
    }

    /**
     * 通过服务器ip打开连接
     *
     * @param id
     * @return
     * @throws AlreadyConnectedException
     * @throws JIException
     * @throws UnknownHostException
     * @throws NotConnectedException
     * @throws DuplicateGroupException
     * @throws AddFailedException
     */
    @GetMapping("/openConnect")
    public Result openConnect(int id) throws Exception {
        if (Blank.isEmpty(id)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return connectService.openConnect(id);
    }

    /**
     * 通过传入服务器id和项名称，读取响应的数据
     *
     * @param id
     * @param itemName
     * @return
     * @throws JIException
     * @throws AddFailedException
     * @throws NotConnectedException
     * @throws AlreadyConnectedException
     * @throws UnknownHostException
     * @throws DuplicateGroupException
     */
    @GetMapping("/readItemValue")
    public Result readItemValue(int id, String itemName) throws Exception {
        if (Blank.isEmpty(id, itemName)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return connectService.readItemValue(id, itemName);
    }

    /**
     * 编辑连接
     *
     * @param serverInformation
     * @return
     */
    @PostMapping("/editConnect")
    public Result editConnect(ServerInformation serverInformation) {
        if (Blank.isEmpty(serverInformation, serverInformation.getIpAddress(), serverInformation.getIpUserName(), serverInformation.getIpPassword(), serverInformation.getAgreementType())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return connectService.editConnect(serverInformation);
    }

    /**
     * 通过id删除保存的连接信息
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteConnect")
    public Result deleteConnect(int id) {
        if (Blank.isEmpty(id)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return connectService.deleteConnect(id);
    }
}
