package com.example.blu.toys.ble.agreement;


import java.text.SimpleDateFormat;
import java.util.Date;

public class TrafficLightBean {
    /**
     * 固定头 1字节
     */
    private byte begin=0x24;

    /**
     * 长度 1字节
     */
    private byte length=0x11;

    /**
     *  协议编号 01：关机协议 02：暂停于继续协议 1字节
     */
    private byte cmd;
    /**
     * 手机时间 yyMMddHHssmm 6个字节
     */
    private byte[] phoneTime;
    /**
     * 暂停继续   0：暂停 1：继续 1字节
     */
    private byte suspendAndContinue;

    /**
     * 剩余时间（小端） 秒为单位 2字节
     */
    private byte[] timeRemaining;

    /**
     * 亮度调节 0-100 1字节
     */
    private byte brightness;

    /**
     * 开机声音编号 1-20 1字节
     */
    private byte openSoundNo;

    /**
     * 关机声音编号 1-20 1字节
     */
    private byte  closeSoundNo;

    /**
     * 红绿灯变更方式 0： 由绿变红 1：由红变黄  1字节
     */
    private byte trafficLightUpdateSet;

    /**
     * 警报开关 0：关 1：开 1字节
     */
    private byte  alarm;



    private byte  key;

    /**
     *
     * @param protocolNo 协议编号
     * @param suspendAndContinue 暂停或者继续
     * @param timeRemaining 剩余时间 毫秒
     * @param brightness 亮度值 0-100 默认 50
     * @param openSoundNo  开机声音编号 默认 1
     * @param closeSoundNo 关机声音编号 默认 10
     * @param trafficLightUpdateSet 红绿灯变更方式  默认 1
     * @param alarm 报警状态 默认 0
     */
    public TrafficLightBean(Integer protocolNo,Integer suspendAndContinue,Integer timeRemaining,Integer brightness,Integer openSoundNo,Integer closeSoundNo,Integer trafficLightUpdateSet,Integer alarm){

        this.cmd= BitOperator.integerTo1Byte(protocolNo);
        this.suspendAndContinue=BitOperator.integerTo1Byte(suspendAndContinue);
        System.out.println("传入的剩余时间："+timeRemaining);
        this.timeRemaining=toLH(timeRemaining); //BitOperator.integerTo2Bytes();
        this.brightness=BitOperator.integerTo1Byte(brightness);
        this.openSoundNo=BitOperator.integerTo1Byte(openSoundNo);
        this.closeSoundNo=BitOperator.integerTo1Byte(closeSoundNo);
        this.trafficLightUpdateSet=BitOperator.integerTo1Byte(trafficLightUpdateSet);
        this.alarm=BitOperator.integerTo1Byte(alarm);
        SimpleDateFormat format= new SimpleDateFormat("yyMMddHHmmss");
        String phoneTime= format.format(new Date());
        this.phoneTime= BCD8421Operater.string2Bcd(phoneTime);
        String  yy= phoneTime.substring(0,2);
        String  MM= phoneTime.substring(2,4);
        String  dd= phoneTime.substring(4,6);
        String  HH= phoneTime.substring(6,8);
        String  mm= phoneTime.substring(8,10);
        String  ss= phoneTime.substring(10,12);

        Integer bo1=BCD8421Operater.string2Bcd(yy)[0]+BCD8421Operater.string2Bcd(HH)[0];
        Integer bo2=BCD8421Operater.string2Bcd(MM)[0]^BCD8421Operater.string2Bcd(mm)[0];
        Integer bo3=BCD8421Operater.string2Bcd(dd)[0]+BCD8421Operater.string2Bcd(ss)[0];

        Integer key=(bo1^bo2)+bo3;
        this.key=BitOperator.integerTo1Byte(key);

        System.out.println("时间："+phoneTime);
        System.out.println("key 10进制："+key);
        System.out.println("key byte："+this.key);

    }

    /**
     * int 转换小端
     * @param n
     * @return
     */
    public static byte[] toLH(int n) {
        byte[] b = new byte[2];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        return b;
    }


    public  byte[] toByte(){
        //数据要加密的
        byte[] resData=new byte[17];
        resData[0]=this.begin;
        resData[1]=this.length;
        resData[2]=BitOperator.integerTo1Byte(this.cmd+this.key);
        resData[3]=this.phoneTime[0];//1 不需要加密
        resData[4]=this.phoneTime[1];//2 不需要加密
        resData[5]=this.phoneTime[2];//3 不需要加密
        resData[6]=this.phoneTime[3];//4 不需要加密
        resData[7]=this.phoneTime[4];//5 不需要急吗
        resData[8]=this.phoneTime[5];//6 不需要加密

        resData[9]=BitOperator.integerTo1Byte(this.suspendAndContinue+this.key);
        resData[10]=BitOperator.integerTo1Byte(this.timeRemaining[0]+this.key);
        resData[11]=BitOperator.integerTo1Byte(this.timeRemaining[1]+this.key);
        resData[12]=BitOperator.integerTo1Byte(this.brightness+this.key);
        resData[13]=BitOperator.integerTo1Byte(this.openSoundNo+this.key);
        resData[14]=BitOperator.integerTo1Byte(this.closeSoundNo+this.key);
        resData[15]=BitOperator.integerTo1Byte(this.trafficLightUpdateSet+this.key);
        resData[16]=BitOperator.integerTo1Byte(this.alarm+this.key);

        System.out.println("发送数据为："+HexStringUtils.toHexString(resData));
        return resData;
    }

    public static void main(String[] args) {

        TrafficLightBean trafficLightBean=new TrafficLightBean(2,0,
                300,100,10,20,01,01);

        System.out.println(HexStringUtils.toHexString(trafficLightBean.toByte()));

        // System.out.printf("00000000");

    }


}