package com.fs.company.controller;

import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.company.domain.CompanyUser;
import com.fs.company.domain.CompanyVoiceApi;
import com.fs.company.domain.CompanyVoiceMobile;
import com.fs.company.param.CompanyVoiceCallerParam;
import com.fs.company.service.ICompanyVoiceApiService;
import com.fs.company.service.ICompanyVoiceMobileService;
import com.fs.company.vo.CompanyVoiceCallerListVO;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.voice.service.IVoiceService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/company/voiceApi")
public class CompanyVoiceApiController extends BaseController {
    @Autowired
    private IVoiceService voiceService;
    @Autowired
    private ICompanyVoiceMobileService mobileService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private ICompanyVoiceApiService companyVoiceApiService;

//    @GetMapping("/callMobile")
//    @RepeatSubmit
//    public R callMobile(
//            @ApiParam(required = false, name = "mobile", value = "被叫号") @RequestParam(value = "mobile", required = false) String mobile,
//            @ApiParam(required = false, name = "customerId", value = "客户ID") @RequestParam(value = "customerId", required = false)Long customerId
//    ) throws Exception
//    {
//        //获取手机号
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        return voiceService.voiceCall(loginUser.getCompany().getCompanyId(),customerId,loginUser.getUser().getUserId(), mobile);
//    }


    /**
     * 查询呼叫接口列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceApi:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyVoiceApi companyVoiceApi) {
        startPage();
        List<CompanyVoiceApi> list = companyVoiceApiService.selectCompanyVoiceApiList(companyVoiceApi);
        return getDataTable(list);
    }


    @ApiOperation("呼叫")
    @GetMapping("/callMobile")
    @RepeatSubmit
    public R callMobile(HttpServletRequest request,
                        @ApiParam(required = false, name = "contactsId", value = "联系人ID") @RequestParam(value = "contactsId", required = false) Long contactsId,
                        @ApiParam(required = false, name = "customerId", value = "客户ID") @RequestParam(value = "customerId", required = false) Long customerId,
                        @ApiParam(required = false, name = "orderId", value = "订单ID") @RequestParam(value = "orderId", required = false) Long orderId,
                        @ApiParam(required = false, name = "callerId", value = "线路接口id") @RequestParam(value = "callerId", required = false) Long callerId
    ) throws Exception
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return voiceService.voiceCall(loginUser.getCompany().getCompanyId(),customerId,contactsId,loginUser.getUser().getUserId(),orderId,callerId);
    }


    @ApiOperation("外呼挂断")
    @GetMapping("callOffMobile")
    public R callOffMobile(@RequestParam("voiceId") Long voiceId) throws Exception
    {
        return voiceService.voiceCallOff(voiceId);
    }

    @ApiOperation("获取SIP")
    @GetMapping(value="/getSipAccount")
    public R getSipAccount(@RequestParam(value = "callerId",required = false) Long callerId) throws Exception
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return voiceService.getSipAccount(loginUser.getUser().getUserId(),callerId);
    }

}
