package com.tcl.ah.tvcontrol.api;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.tcl.customerapi.ICustomerApi;

public class APIHelper {

    private static final String TAG = "APIHelper";
    public static APIHelper apiHelper = null;
    public Context mContext = null;

    public ICustomerApi myInterface = null;
    public static final String ACTION = "action.tvcustomer.api";
    public static final String PACKAGENAME = "com.tcl.customerapi"; //"com.android.finger";
    public static final String PACKAGE_ACT = "com.tcl.customerapi.IService";

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public int hashCode() {
            return super.hashCode();
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myInterface = ICustomerApi.Stub.asInterface(service);
            setPowerOnModeToDirect();
            setFastBootOpen();
            Log.i(TAG, "连接Service 成功");

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "连接Service 失败fail");
        }
    };

    private APIHelper(Context context) {
        mContext = context;
        Intent intent = new Intent(ACTION);
        intent.setPackage(PACKAGENAME);
        //intent.setComponent(new ComponentName(PACKAGENAME,PACKAGE_ACT));
        try {
            mContext.getApplicationContext().bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

    public static APIHelper getInstance(Context ctx) {
        if (apiHelper == null) {
            apiHelper = new APIHelper(ctx);
        }
        return apiHelper;
    }

    public void unBindApiService(){

    }


    public void setFastBootOpen() {
        if (myInterface != null) {
            try {
                myInterface.setFastboot(2);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }else {
            Log.i(TAG, "api is kong");
        }
    }

    public void setAudioMuteStatus(boolean isFlag) {
        if (myInterface != null) {
            try {
                myInterface.setAudioMuteStatus(isFlag);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 设置上电模式
     */
    public void setPowerOnModeToDirect() {
        if (myInterface != null) {
            try {
                myInterface.setPowerOnMode("direct");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }


}