package com.dcsoft.system.uniubi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dcsoft.system.domain.*;
import com.dcsoft.system.kq.domain.*;
import com.dcsoft.system.kq.service.*;
import com.dcsoft.system.service.*;
import com.dcsoft.system.uniubi.domain.Person;
import com.dcsoft.system.uniubi.service.ISysSdkService;
import com.dcsoft.system.uniubi.service.ISysVehicleService;
import com.dcsoft.system.utils.HttpUtil;
import com.dcsoft.system.vehicle.domain.CarInfo;
import com.dcsoft.system.vehicle.service.ICarInfoService;
import com.dcsoft.system.visitor.domain.Visitor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 用户管理 服务层处理
 *
 * @author xueyi
 */
@Service
public class SysVehicleServiceImpl implements ISysVehicleService {
    public static final  String getsnCmd = "{\"cmd\" :\"getsn\"}";
    public static final  String triggerCmd = "{\"cmd\" :\"trigger\"}";
    public static final int DISABLE_PUSH = 0;
    public static final int ENABLE_PUSH = 1;
    public static final int JSON_FMT = 1;
    public static final int BIN_FMT = 0;
    public static final int DISABLE_IMAGE = 0;
    public static final int ENABLE_IMAGE = 1;
    public static final int BLOCK_TYPE_BIN_RESULT = 1;
    public static final int BLOCK_TYPE_IMAGE_DATA = 2;

    @Autowired
    private ICarInfoService carInfoService;

    @Autowired
    private ISysEquipmentService equipmentService;

    //定义一个Socket对象
    private Socket socket = null;

    @Override
    public String config(String ip) {
        String host=ip.split(":")[0];
        int port= Integer.parseInt(ip.split(":")[1]);
        String response="ok";
        try {
            socket = new Socket(host, port);
            sendCmd(socket, getsnCmd);
            configFormat(socket, ENABLE_PUSH, JSON_FMT, ENABLE_IMAGE);
        }catch (IOException e) {
            response=e.getMessage();
        }
        return response;
    }

    @Override
    public String addWhite(SysEquipment equipment, CarInfo carInfo) {
        String ip =equipment.getIp();
        String host=ip.split(":")[0];
        int port= Integer.parseInt(ip.split(":")[1]);
        String response="ok";
        try {
            socket = new Socket(host, port);
            AddWhiteList(socket,carInfo);
        }catch (IOException e) {
            response=e.getMessage();
        }
        return response;
    }

    public  void AddWhiteList(Socket socket,CarInfo cars)
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String create_time =sdf.format(cars.getCreateTime());
        String enable_time =sdf.format(cars.getEnableTime());
        String overdue_time=sdf.format(cars.getOverdueTime());
        String enable=cars.getEnable();
        String plate =cars.getPlate();
        String time_seg_enable =cars.getTimeSegEnable();
        String seg_time=cars.getSegTime();
        String need_alarm=cars.getNeedAlarm();
        String vehicle_code=cars.getVehicleCode();
        String vehicle_comment=cars.getVehicleComment();
        Long customer_id=cars.getCustomerId();
        String cmd = "{"
                + "\"cmd\":\"white_list_operator\","
                + "\"operator_type\":\"update_or_add\","
                + "\"dldb_rec\":{"
                + "\"create_time\":"+create_time+","
                + "\"enable_time\":"+enable_time+","
                + "\"overdue_time\":"+overdue_time+","
                + "\"enable\":"+enable+","
                + "\"plate\":"+plate+","
                + "\"time_seg_enable\":"+time_seg_enable+","
                + "\"seg_time\": "+seg_time+","
                + "\"need_alarm\":"+need_alarm+","
                + "\"vehicle_code\":"+vehicle_code+","
                + "\"vehicle_comment\":"+vehicle_comment+","
                + "\"customer_id\":"+customer_id+""
                + "}"
                + "} ";
        try
        {
            byte[] b = cmd.getBytes("gb2312");//编码
            String sa = new String(b, "gb2312");//解码

            sendCmd(socket,sa);
        }
        catch(Exception e)
        {
            System.out.println("Error:"+e);
        }

    }



    public static boolean configFormat(Socket socket,int enable,int fmt,int image)
    {
        String cmd;
        cmd = String.format("{"+
                        "\"cmd\" : \"ivsresult\"    ,"+
                        "\"enable\" : %s,"+
                        "\"format\" : \"%s\","+
                        "\"image\" : %s"+
                        "}",
                enable!=0?"true":"false",
                fmt !=0 ?"json":"bin",
                image!=0?"true":"false");

        return sendCmd(socket,cmd);
    }

    public static boolean sendCmd(Socket socket,String cmd)
    {
        try{
            int len =  cmd.getBytes().length;
            byte[] header = {'V','Z',0,0,0,0,0,0};
            header[4] += (byte) ((len >>24) & 0xFF);
            header[5] += (byte) ((len >>16) & 0xFF);
            header[6] += (byte) ((len >>8) & 0xFF);
            header[7] += (byte) (len & 0xFF);

            OutputStream out = socket.getOutputStream();
            out.write(header);
            out.write(cmd.getBytes());
        }catch(Exception e)
        {
            System.out.println("Error:"+e.getMessage());
            return false;
        }
        return true;
    }

}
