package com.tcl.factory.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.hdmi.HdmiControlManager;
import android.hardware.hdmi.HdmiTvClient;
import android.provider.Settings;
import android.util.Log;
import com.tcl.tvmanager.*;
import com.tcl.tvmanager.cec.AmplifierImpl;
import com.tcl.tvmanager.vo.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class HDMICecHelper {
    private Context myContext;
    private TTvCecManager tTvManager;
    private AmplifierImpl amplifier;
    private static final int OFF = 0;
    private static final int ON = 1;
    private String TAG = HDMICecHelper.class.getName();
    private static final String HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED ="hdmi_control_auto_device_off_enabled";
    private static final String HDMI_CONTROL_AUTO_WAKEUP_ENABLED ="hdmi_control_auto_wakeup_enabled";
    private static final String HDMI_CONTROL_ENABLED ="hdmi_control_enabled";

    public HDMICecHelper(Context context) {
        myContext = context;
        tTvManager = TTvCecManager.getInstance(myContext);
        amplifier = AmplifierImpl.getInstance(myContext);
    }

    private boolean isAndroidCec() {
        @SuppressLint("WrongConstant") HdmiControlManager hdmiControlManager = (HdmiControlManager) myContext.getSystemService("hdmi_control");
        HdmiTvClient client = hdmiControlManager.getTvClient();
        try {
            return (Boolean) invokeMethod(client, "isAndroidCec", false);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }

    }

    private Object invokeMethod(Object instance, String methodName, Object defaultValue, Object... args) {
        try {
            Class clz = Class.forName(instance.getClass().getName());
            Method method = clz.getMethod(methodName);
            return method.invoke(instance, args);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            Log.e("ReflectUtils", "invokeMethod!", e);
        } catch (NoSuchMethodException ex) {

        } catch (IllegalAccessException ex) {

        } catch (InvocationTargetException ex) {

        }
        return defaultValue;
    }

    private boolean getCECStandbyState() {
        if (isAndroidCec()) {
            return Settings.Global.getInt(myContext.getContentResolver(), HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED, 0) != 0;
        }
        CecConfigInfo cecConfigInfo = tTvManager.getCecConfiguration();
        return cecConfigInfo.autoStandby;
    }

    private boolean getCECPowerOnState() {
        if (isAndroidCec()) {
            return Settings.Global.getInt(myContext.getContentResolver(), HDMI_CONTROL_AUTO_WAKEUP_ENABLED, 0) != 0;
        }
        CecConfigInfo cecConfigInfo = tTvManager.getCecConfiguration();
        return cecConfigInfo.autoPowerOn;
    }

    private void setCECStandbyState(boolean value) {
        if (isAndroidCec()) {
            if (value) {
                Settings.Global.putInt(myContext.getContentResolver(), HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED, ON);
            } else {
                Settings.Global.putInt(myContext.getContentResolver(), HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED, OFF);
            }

        }
        CecConfigInfo cecConfigInfo = tTvManager.getCecConfiguration();
        cecConfigInfo.autoStandby = value;
        tTvManager.setCecConfiguration(cecConfigInfo);
    }

    private void setCECPowerOnState(boolean value) {
        if (isAndroidCec()) {
            if (value) {
                Settings.Global.putInt(myContext.getContentResolver(), HDMI_CONTROL_AUTO_WAKEUP_ENABLED, ON);
            } else {
                Settings.Global.putInt(myContext.getContentResolver(), HDMI_CONTROL_AUTO_WAKEUP_ENABLED, OFF);
            }
            return;
        }
        CecConfigInfo cecConfigInfo = tTvManager.getCecConfiguration();
        cecConfigInfo.autoPowerOn = value;
        tTvManager.setCecConfiguration(cecConfigInfo);
    }

    private void setCECTvFollowStandby(boolean value){
        Log.d(TAG,"setCECTvFollowStandby " + value);
        if(value){
            Settings.Global.putInt(myContext.getContentResolver(), "hdmi_control_auto_tv_off_enabled",ON);
        }else{
            Settings.Global.putInt(myContext.getContentResolver(), "hdmi_control_auto_tv_off_enabled",OFF);
        }

    }

    public void setTLinkEnable(boolean value) {
        if (isAndroidCec()) {
            if (value) {
                Settings.Global.putInt(myContext.getContentResolver(), HDMI_CONTROL_ENABLED, ON);
            } else {
                Settings.Global.putInt(myContext.getContentResolver(), HDMI_CONTROL_ENABLED, OFF);
            }
            if (value) {
                setCECPowerOnState(true);
                setCECStandbyState(true);
                setCECTvFollowStandby(true);
            }
        }
        try {
            if (!value) {
                Log.d(TAG, "value " + value);
                if (getArcConnectStatus() && getArcStates()) {
                    tTvManager.cecPassThrough(TCLKeyMap.EnTCLKeyMap.EN_TCL_KEY_MUTE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        tTvManager.setTLinkEnable(value);
    }

    public boolean getTLinkEnable() {
        if (isAndroidCec()) {
            return Settings.Global.getInt(myContext.getContentResolver(), HDMI_CONTROL_ENABLED, OFF) == ON;
        }
        return tTvManager.isTLinkEnable();
    }

    private boolean getArcConnectStatus() {
        boolean ret = false;
        try {
            if (amplifier != null) {
                ret = amplifier.isSupportARC();
                Log.d(TAG, "getArcConnectStatus() - isSupportARC: "+ret);
            }else {
                Log.e(TAG, "getArcConnectStatus() - mAmplifierImpl != null, something goes wrong!!!");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return ret;
    }

    private boolean getArcStates(){
        Log.d(TAG, "syncArcStates() - ");
        boolean arcMute = false;
        int arcVolume = 0;
        if(amplifier!=null){
            arcMute = amplifier.isMute();
            Log.d(TAG, "getArcMute() - getArcMute:"+arcMute);
            arcVolume = amplifier.getVolume();
            Log.d(TAG, "getArcVolume() - arcVolume:"+arcVolume);
            return arcMute;
        }else {
            Log.e(TAG, "syncArcStates()  - mAmplifierImpl != null, something goes wrong!!!");
        }
        return false;
    }
}
