package com.ericsson.uecontrol.core.util;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;

import com.ericsson.uecontrol.gui.MainActivity;

import org.apache.log4j.Logger;

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

/**
 * Created by ezivkoc on 2014-08-11.
 */
public class CallUtil {
    private static final Logger log = Logger.getLogger(CallUtil.class);


    public static void startCall(String phoneNumber){
        log.debug("Starting Speech Call");
        Context context = MainActivity.getContext();
        Uri number = Uri.parse("tel:" + phoneNumber);

        Intent intent = new Intent();
        //intent.setAction(Intent.ACTION_DIAL);
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(number);
        context.startActivity(intent);
    }

    public static void enableSpeakerPhone(boolean enable) {
        Context context = MainActivity.getContext();
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setSpeakerphoneOn(enable);
    }


    public static void answerCall() throws Exception {
        Context context = MainActivity.getContext();
        log.debug("Answering Speech Call");

        try {
            Object telephonyService = getTelephonyServiceAidl();
            Class c = Class.forName(telephonyService.getClass().getName());
            // Silence ringer
            Method m = c.getDeclaredMethod("silenceRinger");
            m.setAccessible(true);
            m.invoke(telephonyService);
            // Answer Call
            m = c.getDeclaredMethod("answerRingingCall");
            m.setAccessible(true);
            m.invoke(telephonyService);
        }catch (Exception e){
            log.debug("Using fallback call solution");

            // Simulate a press of the headset button to pick up the call
            Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
            buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
            context.sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");

            // froyo and beyond trigger on buttonUp instead of buttonDown
            Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
            buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
            context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
        }
    }


    public static void endCall() throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        log.debug("Ending Speech Call");

        Object telephonyService = getTelephonyServiceAidl();
        Class c = Class.forName(telephonyService.getClass().getName()); // Get its class
        Method m = c.getDeclaredMethod("endCall"); // Get the "endCall()" method
        m.setAccessible(true); // Make it accessible
        m.invoke(telephonyService); // invoke endCall()
    }


    private static Object getTelephonyServiceAidl() throws InvocationTargetException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException {
        Context context = MainActivity.getContext();

        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        return m.invoke(tm);
    }
}
