package com.develop.foodcooking;

import android.app.backup.BackupManager;
import android.content.res.Configuration;
import android.serialport.SerialPort;
import android.util.Log;

import com.develop.food.base.utils.ThreadUtils;
import com.kuyuntech.cofarcooking.device.sdk.eventbus.listener.Dev2McuEventListener;
import com.kuyuntech.cofarcooking.device.sdk.eventbus.listener.Mcu2DevEventListener;
import com.kuyuntech.cofarcooking.device.sdk.protocol.core.BaseDecoder;
import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK;
import com.kuyuntech.cofarcooking.device.sdk.util.core.Converter;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class SerialportUtils {


    public static void init(){


        String serialPortPath = "/dev/ttyS1";
        int baudrate = 9600;
        int dataBits = 8;
        int parity = 0;
        int stopBits = 1;
        int flags = 0 ;



        try {

            SerialPort.setSuPath("/system/xbin/su");

            SerialPort serialPort  = new SerialPort(new File(serialPortPath),baudrate,dataBits,parity,stopBits,flags);

            Dev2McuEventListener dev2McuEventListener = new Dev2McuEventListener(event -> {
                Log.i("dev2mcu:",event.getData());
                byte[] data = Converter.hexStringToBytes(event.getData());
                try {
                    serialPort.getOutputStream().write(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            CofarSDK.register(dev2McuEventListener);

            CofarSDK.register(new Mcu2DevEventListener());

            ProtocolListener protocolListener = new ProtocolListener(serialPort);
            new Thread(protocolListener).start();

            CofarSDK.versionQuery();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public static void updateLanguage(Locale locale) {
        try {
            Object objIActMag;
            Class clzIActMag = Class.forName("android.app.IActivityManager");
            Class clzActMagNative = Class
                    .forName("android.app.ActivityManagerNative");
            //amn = ActivityManagerNative.getDefault();
            Method mtdActMagNative$getDefault = clzActMagNative
                    .getDeclaredMethod("getDefault");
            objIActMag = mtdActMagNative$getDefault.invoke(clzActMagNative);
            // objIActMag = amn.getConfiguration();
            Method mtdIActMag$getConfiguration = clzIActMag
                    .getDeclaredMethod("getConfiguration");
            Configuration config = (Configuration) mtdIActMag$getConfiguration
                    .invoke(objIActMag);
            // set the locale to the new value
            config.locale = locale;
            //持久化  config.userSetLocale = true;
            Class clzConfig = Class
                    .forName("android.content.res.Configuration");
            java.lang.reflect.Field userSetLocale = clzConfig
                    .getField("userSetLocale");
            userSetLocale.set(config, true);
            // 此处需要声明权限:android.permission.CHANGE_CONFIGURATION
            // 会重新调用 onCreate();
            Class[] clzParams = {Configuration.class};
            // objIActMag.updateConfiguration(config);
            Method mtdIActMag$updateConfiguration = clzIActMag
                    .getDeclaredMethod("updateConfiguration", clzParams);
            mtdIActMag$updateConfiguration.invoke(objIActMag, config);
            BackupManager.dataChanged("com.android.providers.settings");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
