package com.yida.data.device.telephone.controller;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.yida.data.device.dto.telephone.PhoneCdataDTO;
import com.yida.data.device.telephone.constant.PhoneConstant;
import com.yida.data.device.telephone.service.PhoneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "亲情电话-话机对接(不鉴权)")
@Validated
@RestController
@RequestMapping("/in/telephone/phone")
@AllArgsConstructor
public class InPhoneController {
    private final PhoneService phoneService;

    @ApiOperation("话机认证")
    @GetMapping("/authen")
    public String authen(@ApiParam("imei号") String sn,
                         @ApiParam("sim卡号") String iccid) {
        return phoneService.authen(sn, iccid);
    }

    @ApiOperation("话机心跳")
    @GetMapping("/getrequest")
    public String getrequest(@ApiParam("imei号") String sn,
                             @ApiParam("sim卡号") String iccid,
                             @ApiParam("版本") String version,
                             @ApiParam("信号等级") Integer signal) {
        phoneService.heartbeat(sn, iccid);
        return PhoneConstant.SUCCESS;
    }

    @ApiOperation("亲情号码读取/获取费率/通话记录")
    @PostMapping("/cdata")
    public String cdata(@ApiParam("imei号") @RequestParam(value = "sn", required = true) String sn,
                        @ApiParam("sim卡号") @RequestParam(value = "iccid", required = true) String iccid,
                        @RequestParam(value = "type", required = false) String type,
                        @ApiParam("页码") @RequestParam(value = "start", required = false) Integer start,
                        @ApiParam("接参") String cardno) {
        log.info("data:[{}]", cardno);
        String[] split = cardno != null ? cardno.split("\t") : new String[0];
        PhoneCdataDTO dto = new PhoneCdataDTO();
        if (split.length > 0) {
            dto.setCardno(split[0]);
            for (String s : split) {
                String[] strings = s.split("=");
                switch (strings[0]) {
                    case "phone":
                        dto.setPhone(strings[1]);
                        break;
                    case "starttime":
                        dto.setStarttime(LocalDateTimeUtil.parse(strings[1], "yyyy-MM-dd HH:mm:ss"));
                        break;
                    case "duration":
                        dto.setDuration(Integer.valueOf(strings[1]));
                        break;
                    case "second":
                        dto.setSecond(Integer.valueOf(strings[1]));
                        break;
                    case "phoneNumber":
                        dto.setPhoneNumber(strings[1]);
                        break;
                    case "money":
                        dto.setMoney(Integer.valueOf(strings[1]));
                        break;
                    default:
                }
            }
        }
        return phoneService.cdata(sn, type, start, dto);
    }

    @ApiOperation("留言读取")
    @GetMapping("/msg_get")
    public String getMsg(String cardno) {
        return phoneService.getMsg(cardno);
    }

    @ApiOperation("留言已读")
    @GetMapping("/msg_read")
    public String readMsg(String cardno, Long id) {
        phoneService.readMsg(id);
        return PhoneConstant.SUCCESS;
    }
}
