package com.tcl.factory.utils;

import com.tcl.tvmanager.TvManager;

import android.Manifest;
import android.annotation.RequiresPermission;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.SystemProperties;
import android.util.Log;

public class AHDeviceHelper {
	public static String getDeviceName(){
        final String PROP_TV_NAME="persist.sys.tvname";
        return SystemProperties.get(PROP_TV_NAME);
    }
    @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
    public static boolean setDeviceName(Context context, String name){
        final String PROP_TV_NAME="persist.sys.tvname";
        final String action_TV_NAME_CHANGED="com.tcl.TVname.change";
        final String CONFIG_BLUTTOOTH="ro.sita.model.Settings.Bluetooth";
        int mMaxLen = 30 ;//data from setting
        if(name == null || name.trim().length()==0 || name.length()>mMaxLen){
            return false ;
        }
        SystemProperties.set(PROP_TV_NAME,name);
        context.sendBroadcast(new Intent(action_TV_NAME_CHANGED));
        if(isShowItem(TvManager.getInstance(context),CONFIG_BLUTTOOTH)){
            BluetoothAdapter.getDefaultAdapter().setName(name);
        }
        return true ;
    }
    public static boolean isShowItem(TvManager tvManager, String key){
        String propertyValue = tvManager.getProperty().get(key,"null");
        if(propertyValue!=null){
            if(propertyValue.equalsIgnoreCase(Boolean.TRUE.toString())) {
                Log.d("device", "propertyValue value=" + propertyValue);
                return true;
            }else if(propertyValue.equalsIgnoreCase(Boolean.FALSE.toString())){
                Log.d("device", "propertyValue value=" + propertyValue);
                return false;
            }
        }
        return true;
    }

}
