package com.tcl.factory.utils;

import android.content.Context;
import android.util.Log;

import com.tcl.customerapi.defs;
import com.tcl.utility.property.PropertyImpl;

public class LedHelper {
    //呼吸灯常量
    private String SITA_PROPERTY_KEY_BOOTUP_LED_STANDBY =
            "sita.bootup.led.standby";  //用于设置/获取待机时,前面板LED灯的状态的property的Key
    private String SITA_PROPERTY_KEY_BOOTUP_LED_WORK =
            "sita.bootup.led.work"; //用于设置/获取整机工作时(即住芯片处于工作状态时),前面板LED灯的状态的property的Key
    private String SITA_PROPERTY_KEY_BOOTUP_LED_STANDBYING = "sita.bootup.led.standbying";
    private String SITA_PROPERTY_KEY_LIGHT_SHADOW_LED = "sita.bootup.rhythmled.work"; //光影呼吸灯
    private String SITA_PROPERTY_VALUE_BOOTUP_LED_ON = "on";          // LED灯亮
    private String SITA_PROPERTY_VALUE_BOOTUP_LED_OFF = "off";         // LED灯熄灭
    private String SITA_PROPERTY_VALUE_BOOTUP_LED_FLASHING = "flashing";     // LED灯闪烁,闪烁频率由Boot决定,暂不做要求
    private String SITA_PROPERTY_VALUE_BOOTUP_LED_BREATHING = "breathing";    // LED灯呈现呼吸效果
    private String SITA_PROPERTY_VALUE_LIGHT_SHADOW_LED_ON = "music_flash";   //光影呼吸灯开
    private String SITA_PROPERTY_VALUE_LIGHT_SHADOW_LED_OFF = "off";   //光影呼吸灯开
    private String PROPERTY_SUPPORT_VALUE = "sys.tcl.bt.driver"; //驱动类型
    private String MTK_7921 = "mtk_7921"; //目前只有7921支持 2021/2/21 增加兼容需要与蓝牙驱动确认 曾庆忠
    private String PROPERTY_DUALMODE_ENABLE = "persist.bluetooth.dualmode.enable"; //音频模式

    private PropertyImpl mPropertyImpl;
    private static final String TAG = "api";

    public LedHelper(Context context) {
        if (mPropertyImpl == null) {
            mPropertyImpl = PropertyImpl.getInstance(context);
        }
    }

    public void initLedMode(int value) {
        switch (value) {
            case defs.FAST_BOOT_OFF:
                if (!isShopMode()) {
                    setBLedShopMode();
                }
                break;
            case defs.FAST_BOOT_ON:
            case defs.FAST_SOUND_BOX:
                if (!isSleepMode()) {
                    setBLDSleepMode();
                }
                break;
            default:
                setBLedShopMode();
                break;
        }
    }

    public boolean setBLedShopMode() {
        //设置呼吸灯商场模式
        if (mPropertyImpl.set(SITA_PROPERTY_KEY_BOOTUP_LED_STANDBY,
                SITA_PROPERTY_VALUE_BOOTUP_LED_BREATHING)) {
            if (mPropertyImpl.set(SITA_PROPERTY_KEY_BOOTUP_LED_WORK,
                    SITA_PROPERTY_VALUE_BOOTUP_LED_ON)) {
                mPropertyImpl.set(SITA_PROPERTY_KEY_BOOTUP_LED_STANDBYING,
                        SITA_PROPERTY_VALUE_BOOTUP_LED_FLASHING);
                Log.i(TAG, "set G90E LED mode is shop and return true");
                return true;
            }
        }
        Log.i(TAG, "set G90E LED mode is shop and return false");
        return false;
    }

    /**
     * 判断呼吸灯当前是否为商场模式
     *
     * @return true为是，false为否
     * @author shenzy
     */
    public boolean isShopMode() {
        String result = mPropertyImpl.get(SITA_PROPERTY_KEY_BOOTUP_LED_WORK,
                "can not getPropertyImpl"
        );
        Log.d(TAG, "SITA_PROPERTY_KEY_BOOTUP_LED_WORK " + result);
        if (result.equals(SITA_PROPERTY_VALUE_BOOTUP_LED_ON)) {
            result = mPropertyImpl.get(SITA_PROPERTY_KEY_BOOTUP_LED_WORK,
                    "can not getPropertyImpl");
            Log.d(TAG, "SITA_PROPERTY_KEY_BOOTUP_LED_STANDBY " + result);
            if (result.equals(SITA_PROPERTY_VALUE_BOOTUP_LED_BREATHING)) {
                Log.d(TAG, "current breathled is shopmode");
                return true;
            }
        }
        Log.d(TAG, "current breathled is not shopmode");
        return false;
    }


    /**
     * 判断呼吸灯当前是否为睡眠模式
     *
     * @return true为是，false为否
     * @author shenzy
     */
    public boolean isSleepMode() {
        String result = mPropertyImpl.get(SITA_PROPERTY_KEY_BOOTUP_LED_WORK, "can not get");
        Log.d(TAG, "SITA_PROPERTY_KEY_BOOTUP_LED_WORK = " + result);
        if (result.equals(SITA_PROPERTY_VALUE_BOOTUP_LED_OFF)) {
            result = mPropertyImpl.get(SITA_PROPERTY_KEY_BOOTUP_LED_STANDBY, "can not get");
            Log.d(TAG, "SITA_PROPERTY_KEY_BOOTUP_LED_STANDBY = " + result);
            if (result.equals(SITA_PROPERTY_VALUE_BOOTUP_LED_OFF)) {
                Log.d(TAG, "current breathled is sleepmode");
                return true;
            }
        }
        Log.d(TAG, "current breathled is not sleepmode");
        return false;
    }

    // 设置呼吸灯睡眠模式
    public boolean setBLDSleepMode() {
        if (mPropertyImpl.set(SITA_PROPERTY_KEY_BOOTUP_LED_STANDBY,
                SITA_PROPERTY_VALUE_BOOTUP_LED_OFF)) {
            if (mPropertyImpl.set(SITA_PROPERTY_KEY_BOOTUP_LED_WORK,
                    SITA_PROPERTY_VALUE_BOOTUP_LED_OFF)) {
                mPropertyImpl.set(SITA_PROPERTY_KEY_BOOTUP_LED_STANDBYING,
                        SITA_PROPERTY_VALUE_BOOTUP_LED_FLASHING);
                Log.i(TAG, "standby: " + mPropertyImpl.get(SITA_PROPERTY_KEY_BOOTUP_LED_STANDBY, "can not get"));
                Log.i(TAG, "work: " + mPropertyImpl.get(SITA_PROPERTY_KEY_BOOTUP_LED_WORK, "can not get"));
                Log.i(TAG, "STANDBYING: " + mPropertyImpl.get(SITA_PROPERTY_KEY_BOOTUP_LED_STANDBYING, "can not get"));
                Log.i(TAG, "set G90E LED mode is sleep and return true");
                return true;
            }
        }
        Log.i(TAG, "set G90E LED mode is sleep and return false");
        return false;
    }
}
