package com.pwt.web.controller;

import com.pwt.common.core.domain.AjaxResult;
import com.pwt.netty.Vehicle2Server;
import com.pwt.netty.common.vehicle2.server.LockRequest;
import com.pwt.netty.common.vehicle2.server.SetParamRequest;
import com.pwt.netty.common.vehicle2.server.UnlockRequest;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/vehicle")
@Slf4j
public class VehicleController {

    @RequestMapping("/list")
    public AjaxResult test() {
        return AjaxResult.success("成功", Vehicle2Server.CHANNEL_MAP.keySet());
    }

    @RequestMapping("/{vin}/lock")
    public AjaxResult lock(@PathVariable String vin){
        Channel channel = Vehicle2Server.CHANNEL_MAP.get(vin);
        if(channel == null || !channel.isActive()) {
            return AjaxResult.error("车辆不在线");
        }
        LockRequest request = new LockRequest();
        request.setDateTime(LocalDateTime.now());
        request.setVin(vin);
        ChannelFuture channelFuture = channel.writeAndFlush(request);

        log.info("发送上锁指令, msg: {}", request);
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                if(channelFuture.cause() == null && channelFuture.isSuccess()) {
                    log.info("发送上锁指令成功");
                } else {
                    log.info("发送上锁指令失败");

                }
            }
        });
        return AjaxResult.success("成功");
    }


    @RequestMapping("/{vin}/unlock")
    public AjaxResult unlock(@PathVariable String vin) {
        Channel channel = Vehicle2Server.CHANNEL_MAP.get(vin);
        if (channel == null || !channel.isActive()) {
            return AjaxResult.error("车辆不在线");
        }
        UnlockRequest request = new UnlockRequest();
        request.setDateTime(LocalDateTime.now());
        request.setVin(vin);
        ChannelFuture channelFuture = channel.writeAndFlush(request);

        log.info("发送解锁指令, msg: {}", request);
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                if (channelFuture.cause() == null && channelFuture.isSuccess()) {

                    log.info("发送解锁指令成功");
                } else {
                    log.info("发送解锁指令失败");
                }
            }
        });

        return AjaxResult.success("成功");
    }


    @RequestMapping("/{vin}/setting")
    public AjaxResult setting(@PathVariable String vin) {
        Channel channel = Vehicle2Server.CHANNEL_MAP.get(vin);
        if (channel == null || !channel.isActive()) {
            return AjaxResult.error("车辆不在线");
        }
        SetParamRequest request = new SetParamRequest();
        request.setDateTime(LocalDateTime.now());
        request.setVin(vin);
        request.setCurrentCEC(111);
        ChannelFuture channelFuture = channel.writeAndFlush(request);

        log.info("发送解锁指令, msg: {}", request);
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                if (channelFuture.cause() == null && channelFuture.isSuccess()) {
                    log.info("发送设置参数指令成功");
                } else {
                    log.info("发送设置参数指令失败");
                }
            }
        });

        return AjaxResult.success("成功");
    }
}
