package com.naz.sdkdemo.weight;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.naz.sdkdemo.bean.UserDetail;
import com.naz.sdkdemo.comm.Constants;
import com.naz.sdkdemo.utils.SPUtils;
import java.io.File;

import android_serialport_api.SerialPortFinder;

/**
 * Description:
 * Author: ydd
 * Date:2020/12/7
 */
public class UserCache extends SPUtils {

    /**
     * 保存用户详情信息
     */
    public static void saveUserInfoDetail(UserDetail userInfo) {
        setString(Constants.USERCACHEKEY, new Gson().toJson(userInfo));
    }

    /**
     * 获取用户详情信息
     */
    public static UserDetail getUserInfoDetail() {
        String str = getString(Constants.USERCACHEKEY);
        if (!TextUtils.isEmpty(str)) {
            return new Gson().fromJson(str, UserDetail.class);
        } else {
            return null;
        }
    }

    /**
     * 清除用户详情数据
     *
     * @return true is success
     */
    public static void clearUserInfoDetail() {
        remove(Constants.USERCACHEKEY);
    }

    public static void saveDeviceId(String code) {
        setString(Constants.DEVICE_CODE, code);
    }

    public static String getDeviceId() {
        String str = getString(Constants.DEVICE_CODE);
        if (!TextUtils.isEmpty(str)) {
            return str;
        } else {
            return  "DV00001";
        }
    }

    public static void saveSerialInfo(String ip, int port, String name, int rate) {
        setString(Constants.IP, ip);
        setInt(Constants.PORT, port);
        setString(Constants.SERIAL_NAME, name);
        setInt(Constants.SERIAL_RATE, rate);
    }
    public static String getIP() {
        String str = getString(Constants.IP);
        if (!TextUtils.isEmpty(str)) {
            return str;
        } else {
            return  "192.168.1.253";
        }
    }

    public static int getPort() {
        int value = getInt(Constants.PORT);
        if (value == -1){
            value = 1030;
        }
        return value;
    }

    public static int getSerialRate() {
        int value = getInt(Constants.SERIAL_RATE);
        if (value == -1){
            value = 19200;
        }
        return value;
    }

    public static String getSerialName() {

        SerialPortFinder finder = new SerialPortFinder();

        searchDevDirectory(new File("/dev"));
        if (finder.getAllDevicesPath().length>0){
            for (int i = 0; i < finder.getAllDevicesPath().length; i++) {
                Log.e("------------>","---------->finder.getAllDevicesPath("+i+")="+finder.getAllDevicesPath()[i]);
            }
            Log.e("------------>","---------->finder.getAllDevicesPath()="+finder.getAllDevicesPath());
            String sName = finder.getAllDevicesPath()[0];
            return sName;
        }
        return "";
    }

    private static void searchDevDirectory(File directory) {
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        // 递归搜索子目录
                        searchDevDirectory(file);
                    } else {
                        // 检查是否是串口设备文件
                        if (file.getAbsolutePath().startsWith("/dev/tty")) {
                            System.out.println("Serial port found: " + file.getAbsolutePath());
                        }
                    }
                }
            }
        }
    }

    public static String getSerialPortName() {

        SerialPortFinder finder = new SerialPortFinder();

        if (finder.getAllDevicesPath().length>0){
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < finder.getAllDevicesPath().length; i++) {
                str.append(finder.getAllDevicesPath()[i]);
                if (!(i==0 || i==finder.getAllDevicesPath().length-1)){
                    str.append(",");
                }
                Log.e("------------>","---------->finder.getAllDevicesPath("+i+")="+finder.getAllDevicesPath()[i]);
            }
            Log.e("------------>","---------->finder.getAllDevicesPath()="+finder.getAllDevicesPath());
            return str.toString();
        }
        return "";
    }
}
