package org.example.socket.utils;

import org.springframework.stereotype.Component;

@Component
public class CommonConfig {

    public static final String electric0 = "127.0.0.1";

    public static final String electric1 = "10.29.16.202";

    public static final String electric2 = "10.29.16.244";

    public int [] sendElectricInstruct (String ip,int fn) {
        int [] arr = new int[20];
        arr[0] = 0x68;
        arr[1] = 0x00;
        arr[2] = 0x00;
        arr[3] = 0x00;
        arr[4] = 0x00;
        arr[5] = 0x68;
        arr[6] = 0x4B;
        arr[7] = 0x22;  // 地址域
        arr[8] = 0x20;
        arr[9] = 0xFE;
        arr[10] = 0x52;
        arr[11] = 0x00;
        arr[12] = 0x0C;
        arr[13] = 0x70;
        arr[14] = 0xFF;  // Pn
        arr[15] = 0x00;
        arr[16] = 0x00;  //Fn
        arr[17] = 0x00;
        arr[18] = 0x00;
        arr[19] = 0x16;
        getAddress(arr,ip,fn);
        getCSAndLength(arr);
        return arr;
    }

    public int [] sendElectricInstruct2 (String ip,int fn) {
        int [] arr = new int[20];
        arr[0] = 0x68;
        arr[1] = 0x00;
        arr[2] = 0x00;
        arr[3] = 0x00;
        arr[4] = 0x00;
        arr[5] = 0x68;
        arr[6] = 0x4B;
        arr[7] = 0x22;  // 地址域
        arr[8] = 0x20;
        arr[9] = 0xFE;
        arr[10] = 0x52;
        arr[11] = 0x00;
        arr[12] = 0x00;
        arr[13] = 0x70;
        arr[14] = 0x00;  // Pn
        arr[15] = 0x00;
        arr[16] = 0x01;  //Fn
        arr[17] = 0x00;
        arr[18] = 0x00;
        arr[19] = 0x16;
        getAddress(arr,ip,fn);
        getCSAndLength(arr);
        return arr;
    }


    public void getAddress (int [] arr,String ip,int fn) {
        switch (ip) {
            case electric1:
                arr[7] = 0x22;
                arr[8] = 0x20;
                arr[9] = 0xFE;
                arr[10] = 0x52;
                arr[11] = 0x00;
                break;
            case electric2:
                arr[7] = 0x22;
                arr[8] = 0x20;
                arr[9] = 0xFD;
                arr[10] = 0x52;
                arr[11] = 0x00;
                break;
        }
        arr[16] = get8421(fn - (fn / 8) * 8);
        arr[17] = fn / 8;
    }

    public int get8421(int DT1) {
        int dt = 00;
        switch (DT1) {
            case 1 : dt = 0x01; break;
            case 2 : dt = 0x02; break;
            case 3 : dt = 0x04; break;
            case 4 : dt = 0x08; break;
            case 5 : dt = 0x10; break;
            case 6 : dt = 0x20; break;
            case 7 : dt = 0x40; break;
            case 8 : dt = 0x80; break;
        }
        return dt;
    }

    public void getCSAndLength (int [] arr) {
        arr[1] =Integer.parseInt(Integer.toHexString(Integer.parseInt(Integer.toBinaryString(arr.length - 8) + "10", 2)),16);
        arr[3] = arr[1];
        for (int i = 6; i < arr.length - 2; i++) {
                arr[arr.length - 2] += arr[i];
        }
        if(arr[arr.length - 2] > 255) {
            String s = Integer.toHexString(arr[arr.length - 2]);
            arr[arr.length - 2] = Integer.parseInt(s.substring(s.length() - 2,s.length()),16);
        }
    }

}
