/*
 * Copyright (c) 2016 Route1 Inc.  All rights reerved.  <sales@route1.com>
 * http://www.route1.com
 *
 * You may use, distribute and modify this code under the
 * terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This code is provided “as -is,” without warranty of any kind,
 * express, implied or statutory, including but not limited to
 * any implied warranties or conditions of quality, performance,
 * merchantability, merchantable quality, durability,
 * non-infringement or of fitness for a particular purpose.
 * See the GPLv2 for more details.
 *
 * Contributor:  A. M. Jehad Sarkar  <amjehad.sarkar@route1.com>
*/

package com.routeone.dc.vpn;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.Log;

@SuppressWarnings("WeakerAccess")
public final class DcContainerInterface {

    private static final String TAG                            = "DcContainerInterface";
    public static final  String ACTION_CONTAINER_PUB_INTERFACE = "containerapp.PUBLIC_INTERFACE";
    public static final  String ACTION_CONTAINER_RESPONSE      = "containerapp.RESPONSE";
    public static final  String ACTION_ERROR                   = "containerapp.ERROR";
    public static final  String DC_CONTAINER_PKG               = "com.routeone.dc.containerapp";
    public static final  String ARG_MESSAGE                    = "arg.message";
    public static final  String ARG_PRIVATE_KEY                = "privateKey";
    public static final  String ARG_CERT_CHAIN                 = "certificateChain";
    public static final  String ARG_VPN_CONFIG                 = "configuration";
    public static final  String ARG_PIN_VERIFIED               = "pin_verified";
    public static final  String ARG1_STRING                    = "arg1.string";
    public static final  String ARG2_STRING                    = "arg2.string";
    public static final  String ARG3_STRING                    = "arg3.string";
    public static final  String ARG4_STRING                    = "arg4.string";
    public static final  String ARG0_SENDER_PKG                = "arg0.sender.package";
    public static final  int    MSG_VERIFY_PIN                 = 301;
    public static final  int    MSG_ON_PIN_VERIFIED            = 302;
    public static final  int    MSG_RETRIEVE_CREDENTIAL        = 303;
    public static final  int    MSG_ON_RETRIEVE_CREDENTIAL     = 304;
    public static final  int    MSG_RETRIEVE_VPN_CONFIGURATION = 320;
    public static final  int    MSG_ON_RETRIEVE_VPN_CONFIGURATION = 321;
    public static final  int    MSG_ON_ERROR                   = 911;

    public static boolean isMyServiceRunning(@NonNull Context context, Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }



    public static void verifyPin(@NonNull Activity activity, @NonNull String pin) {
        Context context = activity.getApplicationContext();
        if(DcUtils.isPackageInstalled(DcContainerInterface.DC_CONTAINER_PKG, context.getPackageManager())) {
            Log.d(TAG, "verifyPin()");
            //Intent intent = new Intent().setAction(DcContainerInterface.ACTION_CONTAINER_PUB_INTERFACE);
            //intent.setPackage(context.getApplicationInfo().packageName);
            //intent.setClassName("com.routeone.dc.containerapp", "com.routeone.dc.containerapp.service.ContainerService");
            //context.startService(intent);
            context.sendBroadcast(new Intent().setAction(DcContainerInterface.ACTION_CONTAINER_PUB_INTERFACE)
                    .putExtra(DcContainerInterface.ARG_MESSAGE, DcContainerInterface.MSG_VERIFY_PIN)
                    .putExtra(DcContainerInterface.ARG1_STRING, pin)
                    .putExtra(DcContainerInterface.ARG0_SENDER_PKG, context.getApplicationInfo().packageName)
                    .setPackage(DcContainerInterface.DC_CONTAINER_PKG));
        }
        else
        {
            DcUtils.showErrorDialog(activity, "Please make sure the DerivID App is installed.");
        }
    }

    public static void requestCredentials(@NonNull Context context, @NonNull String credentialType, @NonNull String pin) {
        if(DcUtils.isPackageInstalled(DcContainerInterface.DC_CONTAINER_PKG, context.getPackageManager())) {
            Log.d(TAG, "requestCredentials()");
            context.sendBroadcast(new Intent().setAction(DcContainerInterface.ACTION_CONTAINER_PUB_INTERFACE)
                    .putExtra(DcContainerInterface.ARG_MESSAGE, DcContainerInterface.MSG_RETRIEVE_CREDENTIAL)
                    .putExtra(DcContainerInterface.ARG1_STRING, credentialType)
                    .putExtra(DcContainerInterface.ARG4_STRING, pin)
                    .putExtra(DcContainerInterface.ARG0_SENDER_PKG, context.getApplicationInfo().packageName)
                    .setPackage(DcContainerInterface.DC_CONTAINER_PKG));
        }
        else
        {
            if((Activity)context != null)
                DcUtils.showErrorDialog((Activity)context, "Please make sure the DerivID App is installed.");
        }
    }

    public static void requestConfiguration(@NonNull Context context, @NonNull String from, @NonNull String type, @NonNull String pin) {
        Log.d(TAG, "requestVPNConfig()");
        context.sendBroadcast(new Intent().setAction(DcContainerInterface.ACTION_CONTAINER_PUB_INTERFACE)
                .putExtra(DcContainerInterface.ARG_MESSAGE, DcContainerInterface.MSG_RETRIEVE_VPN_CONFIGURATION)
                .putExtra(DcContainerInterface.ARG2_STRING, from)
                .putExtra(DcContainerInterface.ARG3_STRING, type)
                .putExtra(DcContainerInterface.ARG4_STRING, pin)
                .putExtra(DcContainerInterface.ARG0_SENDER_PKG, context.getApplicationInfo().packageName)
                .setPackage(DcContainerInterface.DC_CONTAINER_PKG));
    }

    private DcContainerInterface() {
    }
}