package com.unis.detectionnum.server.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.unis.detectionnum.server.message.utils.MySecureProtocolSocketFactory;
import com.unis.detectionnum.server.schedule.pojo.CustomerPersonImportVO;
import com.unis.detectionnum.server.schedule.pojo.RobotCallJobPO;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;

import java.time.LocalTime;
import java.util.*;

public class RobotCallJobUtils {


//    public static String APP_KEY = "PPaDQMgmIUmYbYCA";
//    public static String APP_SECRET = "JSXVjjHNlXslhBNLCMylfxsIWUbABfak";
    public static String APP_KEY = "uAWGuQzCqgMqsRyp";
    public static String APP_SECRET = "qhbTKNFdbwDKrjpthRhaeQAFnyHPLOIh";

    public static final String TENANT_SIGN = "ziguangsoft";
//    public static final String TENANT_SIGN = "ceshiduli";



    public static final String VERSION = "v1";

    //    public static final String URL = "https://openapi.tanyibot.com";
//    public static final String URL = "https://crm.yiwise.com";
    public static final String URL = "https://openapi.tanyibot.com";



    /**
     * 公司列表
     * @return
     */
    public static void getTenant() {
        String url = URL+"/apiOpen/v1/tenant/getTenant";
        Long timestamp = System.currentTimeMillis();
        String result = HttpUrlConnectionUtils.doGet(url, APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }

    /**
     * 电话线列表
     * @return
     */
    public static void getPhoneList() {
        String url = URL+"/apiOpen/v1/phone/getPhoneList";
        Long timestamp = System.currentTimeMillis();
        String result = HttpUrlConnectionUtils.doGet(url, APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }
    /**
     * 电话线列表
     * @return
     */
    public static void updatePhoneInfoByTenantPhoneNumberId() {
        String url = URL+"/apiOpen/v1/phone/updatePhoneInfoByTenantPhoneNumberId";
        Long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("tenantPhoneNumberId", 1010);
        jsonObject.put("areaCode", "010");
        jsonObject.put("province", "北京市");
        jsonObject.put("city", "朝阳区");
        JSONArray jsonArray = new JSONArray();
        JSONObject deadArea = new JSONObject();
        deadArea.put("prov", "xx省");
        deadArea.put("city", "xx市");
        jsonArray.add(deadArea);
        jsonObject.put("deadArea", jsonArray);
        jsonObject.put("callOutIndustry", "FINANCE");
        System.out.println(jsonObject.toJSONString());
        String result = HttpUrlConnectionUtils.doPost(url, jsonObject.toJSONString(), APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }
    /**
     * 电话线列表
     * @return
     */
    public static void updatePhonePriceByTenantPhoneNumberId() {
        String url = URL+"/apiOpen/v1/phone/updatePhonePriceByTenantPhoneNumberId";
        Long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("tenantPhoneNumberId", 1010);
        jsonObject.put("localBillRate", 9999);
        jsonObject.put("otherBillRate", 9999);
        System.out.println(jsonObject.toJSONString());
        String result = HttpUrlConnectionUtils.doPost(url, jsonObject.toJSONString(), APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }

    /**
     * 机器人话术列表
     * @return
     */
    public static void getDialogFlowList() {
        String url = URL+"/apiOpen/v1/dialogFlow/getDialogFlowList";
        Long timestamp = System.currentTimeMillis();
        String result = HttpUrlConnectionUtils.doGet(url, APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }

    /**
     * 创建任务
     * @return
     */
    public static void updateIsvInfo()  {
        String url = URL+"/apiOpen/v1/isv/updateIsvInfo";
        Long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("tenantSign", "yiwise");
        jsonObject.put("callbackUrl", "http:999999999");
        String result = HttpUrlConnectionUtils.doPost(url, jsonObject.toJSONString(), APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }

    /**
     * 获取任务列表
     * @return
     */
    public static void getJobs() {
        String url = URL+"/apiOpen/v1/job/getJobs";
        Long timestamp = System.currentTimeMillis();
//        try {
//            url = url+"?name="+ URLEncoder.encode("测试","utf-8") +"&status=IN_PROCESS"+"&pageNum=1"+"&pageSize=20";
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        String result = HttpUrlConnectionUtils.doGet(url, APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }
    /**
     * 获取某个任务信息
     * @return
     */
    public static void getJobDetail() {
        String url = URL+"/apiOpen/v1/job/getJobDetail";
        Long timestamp = System.currentTimeMillis();
        url = url+"?robotCallJobId=1470";
        String result = HttpUrlConnectionUtils.doGet(url, APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }
    /**
     * 获取某个任务信息
     * @return
     */
    public static void getJobStats() {
        String url = URL+"/apiOpen/v1/job/getJobStats";
        Long timestamp = System.currentTimeMillis();
        url = url+"?robotCallJobId=1470";
        String result = HttpUrlConnectionUtils.doGet(url, APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }
    /**
     * 获取某个任务信息
     * @return
     */
    public static void getJobProperties() {
        String url = URL+"/apiOpen/v1/job/getJobProperties";
        Long timestamp = System.currentTimeMillis();
        url = url+"?robotCallJobId=14965";
        String result = HttpUrlConnectionUtils.doGet(url, APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }
    /**
     * 获取已呼电话列表
     * @return
     */
    public static void getCallRecordInfoList() {
        String url = URL+"/apiOpen/v1/callRecord/getCallRecordInfoList";
        Long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("robotCallJobId", 14965);
//        jsonObject.put("searchWords", "测试");
//        Set<String> dialStatusEnumSet = new HashSet<>();
//        dialStatusEnumSet.add("ANSWERED");
//        jsonObject.put("resultStatuses", dialStatusEnumSet);
        Set<String> intentLevelsEnumSet = new HashSet<>();
        intentLevelsEnumSet.add("B");
        jsonObject.put("intentLevels", intentLevelsEnumSet);
//        jsonObject.put("dialogFlowId", 2);
//        jsonObject.put("pageNum", 1);
//        jsonObject.put("pageSize", 20);
//        System.out.println(jsonObject.toJSONString());
        String result = HttpUrlConnectionUtils.doPost(url, jsonObject.toJSONString(), APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }
    /**
     * 获取通话记录详情
     * @return
     */
    public static void callDetail() {
        String url = URL+"/a" +
                "piOpen/v1/callRecord/callDetail";
        Long timestamp = System.currentTimeMillis();
        url = url+"?callRecordId=58544416";
        String result = HttpUrlConnectionUtils.doGet(url, APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }
    /**
     * 创建任务
     * @return
     */
    public static String createTask()  {
        String url = URL+"/apiOpen/v1/job/create";
        Long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        RobotCallJobPO robotCallJobPO = new RobotCallJobPO();
        //robotCallJobPO.setRobotCallJobId(100L);
        //robotCallJobPO.setTenantId(73L);
//        robotCallJobPO.setDialogFlowId(8071L);
        robotCallJobPO.setDialogFlowId(8140L);
        robotCallJobPO.setPrior(true);
        robotCallJobPO.setName("紫光软件");
        robotCallJobPO.setMode("MANUAL");
        robotCallJobPO.setRobotCount(1);
        robotCallJobPO.setDailyStartTime(LocalTime.of(9, 0));
        robotCallJobPO.setDailyEndTime(LocalTime.of(20, 0));
//        robotCallJobPO.setCsStaffGroupId(12945L);
        /*robotCallJobPO.setInactiveStartTime(LocalTime.of(12, 0));
        robotCallJobPO.setInactiveEndTime(LocalTime.of(13, 0));*/

        robotCallJobPO.setDescription("测试");
        //robotCallJobPO.setSmsTemplateId(1L);
        robotCallJobPO.setPhoneType("LANDLINE");
        jsonObject.put("concurrencyQuota", 1);
        jsonObject.put("robotCallJob", robotCallJobPO);
        List<Long> longStringBOS = new ArrayList<>();
        longStringBOS.add(18461L);//电话列表  id
//        longStringBOS.add(12945L);//电话列表  id
        jsonObject.put("jobPhoneNumberIdList",longStringBOS);
//        System.out.println(jsonObject.toJSONString());
        String result = HttpUrlConnectionUtils.doPost(url, jsonObject.toJSONString(), APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
       /* Set<String> wechatAlertLevel = new HashSet<>();
        robotCallJobPO.setWechatAlertLevel(wechatAlertLevel);
        Set<String> smsAlertLevel = new HashSet<>();
        smsAlertLevel.add("A");
        robotCallJobPO.setSmsAlertLevel(smsAlertLevel);
        Set<Long> alertUsers = new HashSet<>();
        robotCallJobPO.setAlertUsers(alertUsers);
        Set<Long> earlyWarning = new HashSet<>();
        robotCallJobPO.setEarlyWarningAlertUsers(earlyWarning);*/

        //robotCallJobPO.setStartTime("2018-11-07 05:05:05");
        //robotCallJobPO.setWechatSendMethod("SENDTOALL");
        return result;
    }
    /**
     * 删除任务
     * @return
     */
    public static void deleteTask(String telephone)  {
        String url = "https://robot.yiwise.com/apiOpen/v1/job/delete";
        Long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("robotCallJobId", telephone);
        String result = HttpUrlConnectionUtils.doPost(url, jsonObject.toJSONString(), APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }
    /**
     * 开启任务
     * @return
     */
    public static void startTask(String telephone)  {
        String url = URL+"/apiOpen/v1/job/start";
        Long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("robotCallJobId", telephone);
        String result = HttpUrlConnectionUtils.doPost(url, jsonObject.toJSONString(), APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }

    /**
     * 开启任务
     * @return
     */
    public static void pauseTask(String telephone)  {
        String url = URL+"/apiOpen/v1/job/pause";
        Long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("robotCallJobId", telephone);
        String result = HttpUrlConnectionUtils.doPost(url, jsonObject.toJSONString(), APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }

    /**
     * 开启任务
     * @return
     */
    public static void stopTask(String telephone)  {
        String url = URL+"/apiOpen/v1/job/stop";
        Long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("robotCallJobId", telephone);
        String result = HttpUrlConnectionUtils.doPost(url, jsonObject.toJSONString(), APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }

    /**
     * 向任务中导入客户
     * @return
     */
    public static void importTaskCustomer(String telephone,String tel,String cfmc)  {
        String url = URL+"/apiOpen/v1/job/importCustomer";
        Long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        List<CustomerPersonImportVO> customerPersons = new ArrayList<>();
        CustomerPersonImportVO customerPersonImportVO = new CustomerPersonImportVO();
        customerPersonImportVO.setName("kh");
        customerPersonImportVO.setPhoneNumber(tel);
        //customerPersonImportVO.setPhoneNumber("18501020163");
        //customerPersonImportVO.setPhoneNumber("18910102780");
        //添加变量
        Map<String, String> properties = new HashMap<>();
        properties.put("公司名称", "商丘直属库");
//        properties.put("仓号", "1号");
        properties.put("仓号", cfmc);
        customerPersonImportVO.setProperties(properties);

        customerPersons.add(customerPersonImportVO);
        jsonObject.put("robotCallJobId", telephone);
        jsonObject.put("customerPersons", customerPersons);
//        System.out.println(jsonObject.toJSONString());
        String result = HttpUrlConnectionUtils.doPost(url, jsonObject.toJSONString(), APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }

    /**
     * 修改任务并发数
     * @return
     */
    public static void updateTaskAiCount()  {
        String url = URL+"/apiOpen/v1/job/updateAiCount";
        Long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("robotCallJobId", 14965);
        jsonObject.put("robotCount", 1);
        String result = HttpUrlConnectionUtils.doPost(url, jsonObject.toJSONString(), APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }

    public static void oneTimeCall() {
        String url = URL+"/apiOpen/v1/job/oneTimeCall";
        Long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dialogFlowId", 5474L);
        List<Long> jobPhoneNumberIdList = new ArrayList<>();
        jobPhoneNumberIdList.add(1060L);
        jsonObject.put("jobPhoneNumberIdList", jobPhoneNumberIdList);
        jsonObject.put("phoneType", "LANDLINE");

        List<CustomerPersonImportVO> customerPersons = new ArrayList<>();
        CustomerPersonImportVO customerPersonImportVO = new CustomerPersonImportVO();
        customerPersonImportVO.setName("小红羊");
        customerPersonImportVO.setPhoneNumber("18501020163");
        customerPersons.add(customerPersonImportVO);
        jsonObject.put("customerPersons", customerPersons);

        String result = HttpUrlConnectionUtils.doPost(url, jsonObject.toJSONString(), APP_KEY, APP_SECRET, TENANT_SIGN, VERSION, timestamp.toString());
        System.out.println(result);
    }
}
