package com.tuyoo.sdk;

import android.content.Intent;
import android.os.Bundle;

import com.tuyoo.sdk.sdk_inter_layer_unity.UnityInterface;

import android.util.Log;
import com.google.android.games.vkquality.VKQuality;

public class Activity extends ${0} {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        CheckVkQuality();

        super.onCreate(savedInstanceState);

        UnityInterface.onActivityCreate(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        UnityInterface.onActivityResume(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
        UnityInterface.onActivityStart(this);

    }

    @Override
    protected void onStop() {
        super.onStop();
        UnityInterface.onActivityStop(this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        UnityInterface.onActivityPause(this);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UnityInterface.onResult(this, requestCode, resultCode, data);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        UnityInterface.onNewIntent(this, intent);
    }


    private static final int OVERRIDE_NONE = 0;
    private static final int OVERRIDE_GLES = 1;
    private static final int OVERRIDE_VULKAN = 2;

    private int apiOverride = OVERRIDE_NONE;

    @Override protected String updateUnityCommandLineArguments(String cmdLine)
    {
        if (apiOverride == OVERRIDE_VULKAN) {
            Log.i("VKQUALITY", "Passing -force-vulkan");
            return appendCommandLineArgument(cmdLine, "-force-vulkan");
        }
        else if (apiOverride == OVERRIDE_GLES) {
            Log.i("VKQUALITY", "Passing -force-gles");
            return appendCommandLineArgument(cmdLine, "-force-gles");
        }
        Log.i("VKQUALITY", "No override passed");
        // let Unity pick the Graphics API based on PlayerSettings
        return cmdLine;
    }    

    private void CheckVkQuality() {
        VKQuality vkQuality = new VKQuality(this);
        // Currently defined flags (bitfields, can be OR'ed together):
        // Recommend to just pass 0 unless you know why you want to set a flag
        // VkQuality.INIT_FLAG_SKIP_STARTUP_MITIGATION = 1
        //   If set, this will skip the startup crash mitigation path
        // VkQuality INIT_FLAG_GLES_ONLY_STARTUP_MITIGATION_DEVICES = 2
        //   If set, this will only return GLES and never Vulkan for
        //   devices affected by the startup crash mitigation path
		// VkQuality INIT_FLAG_SKIP_DRIVER_FINGERPRINT_CHECK = 4
		//   If set, this will disable a check that uses a list
		//   of SoC/driver fingerprint pairs to predict Vulkan quality
        int startResult = vkQuality.StartVkQualityWithFlags("", 0);
        if (startResult == VKQuality.INIT_SUCCESS) {
            // In the current release, we can assume GetVkQuality is
            // ready as soon as StartVkQuality has returned success
            int getResult = vkQuality.GetVkQuality();
            LogVkQualityResult(getResult);

            switch (getResult) {
                case VKQuality.RECOMMENDATION_VULKAN_BECAUSE_DEVICE_MATCH:
                case VKQuality.RECOMMENDATION_VULKAN_BECAUSE_PREDICTION_MATCH:
                case VKQuality.RECOMMENDATION_VULKAN_BECAUSE_FUTURE_ANDROID:
                    apiOverride = OVERRIDE_VULKAN;
                    break;
                case VKQuality.RECOMMENDATION_GLES_BECAUSE_OLD_DEVICE:
                case VKQuality.RECOMMENDATION_GLES_BECAUSE_OLD_DRIVER:
                case VKQuality.RECOMMENDATION_GLES_BECAUSE_NO_DEVICE_MATCH:
                case VKQuality.RECOMMENDATION_GLES_BECAUSE_PREDICTION_MATCH:                
                default:
                    apiOverride = OVERRIDE_GLES;
                    break;
            }
            vkQuality.StopVkQuality();
        } else {
            LogVkQualityError(startResult);
        }
    }

    private String LogVkQualityResult(int getResult) {
        String returnString = "";
        switch (getResult) {
            case VKQuality.RECOMMENDATION_NOT_READY:
                returnString = "RECOMMENDATION_NOT_READY";
                break;
            case VKQuality.RECOMMENDATION_ERROR_NOT_INITIALIZED:
                returnString = "RECOMMENDATION_ERROR_NOT_INITIALIZED";
                break;
            case VKQuality.RECOMMENDATION_VULKAN_BECAUSE_DEVICE_MATCH:
                returnString = "RECOMMENDATION_VULKAN_BECAUSE_DEVICE_MATCH";
                break;
            case VKQuality.RECOMMENDATION_VULKAN_BECAUSE_PREDICTION_MATCH:
                returnString = "RECOMMENDATION_VULKAN_BECAUSE_PREDICTION_MATCH";
                break;
            case VKQuality.RECOMMENDATION_VULKAN_BECAUSE_FUTURE_ANDROID:
                returnString = "RECOMMENDATION_VULKAN_BECAUSE_FUTURE_ANDROID";
                break;
            case VKQuality.RECOMMENDATION_GLES_BECAUSE_OLD_DEVICE:
                returnString = "RECOMMENDATION_GLES_BECAUSE_OLD_DEVICE";
                break;
            case VKQuality.RECOMMENDATION_GLES_BECAUSE_OLD_DRIVER:
                returnString = "RECOMMENDATION_GLES_BECAUSE_OLD_DRIVER";
                break;
            case VKQuality.RECOMMENDATION_GLES_BECAUSE_NO_DEVICE_MATCH:
                returnString = "RECOMMENDATION_GLES_BECAUSE_NO_DEVICE_MATCH";
                break;
            case VKQuality.RECOMMENDATION_GLES_BECAUSE_PREDICTION_MATCH:
                returnString = "RECOMMENDATION_GLES_BECAUSE_PREDICTION_MATCH";
                break;
            default:
                returnString = "Unknown getVkQuality result: " + getResult;
                break;
        }
        Log.i("VKQUALITY", returnString);
        return returnString;
    }

    private String LogVkQualityError(int startResult) {
        String returnString = "";
        switch (startResult) {
            case VKQuality.ERROR_INITIALIZATION_FAILURE:
                returnString = "ERROR_INITIALIZATION_FAILURE";
                break;
            case VKQuality.ERROR_NO_VULKAN:
                returnString = "ERROR_NO_VULKAN";
                break;
            case VKQuality.ERROR_INVALID_DATA_VERSION:
                returnString = "ERROR_INVALID_DATA_VERSION";
                break;
            case VKQuality.ERROR_INVALID_DATA_FILE:
                returnString = "ERROR_INVALID_DATA_FILE";
                break;
            case VKQuality.ERROR_MISSING_DATA_FILE:
                returnString = "ERROR_MISSING_DATA_FILE";
                break;
            default:
                returnString = "Unknown startVkQuality result: " + startResult;
                break;
        }
        Log.e("VKQUALITY", returnString);
        return returnString;
    }

    private String appendCommandLineArgument(String cmdLine, String arg) {
        if (arg == null || arg.isEmpty())
            return cmdLine;
        else if (cmdLine == null || cmdLine.isEmpty())
            return arg;
        else
            return cmdLine + " " + arg;
    }
}
