package proca

import (
	"fmt"
	"strings"

	"android/soong/android"
	"android/soong/cc"
)

func procalibDefaults(ctx android.LoadHookContext) {
	type props struct {
		Cflags      []string
		Srcs []string
		Target      struct {
			Android struct {
				Required []string
			}
		}
	}

	p := &props{}

	proca_enabled, ok_en := ctx.AConfig().Getspf("SEC_PRODUCT_FEATURE_SECURITY_SUPPORT_PROCA")
	proca_version, ok_ver := ctx.AConfig().Getspf("SEC_PRODUCT_FEATURE_SECURITY_CONFIG_PROCA_VERSION")
	sssi_target, _ := ctx.AConfig().Getspf("SEC_PRODUCT_FEATURE_BUILD_SSSI_TARGET")

	if ok_en && proca_enabled == "TRUE" {
		if sssi_target != "system" {
			p.Target.Android.Required = []string{"vendor.samsung.hardware.security.proca@2.0-service"}
		}
		if ok_ver && strings.HasPrefix(proca_version, "1.") {
			//leave default value, because 1.x proca versions share same library
			fmt.Println("[PROCA] library version 1.x")
		} else {
			fmt.Printf("[PROCA] library version %s\n", proca_version)
		}
		p.Srcs = append(p.Srcs, ":proca_libpa_sources")
	} else {
		fmt.Println("[PROCA] PROCA is disabled. Daemon is not needed.")
		p.Srcs = append(p.Srcs, ":proca_libpa_sources_placeholder")
	}

	use_kinibi, _ := ctx.AConfig().GetMkVar("USE_MOBICORE")
	use_teegris, _ := ctx.AConfig().GetMkVar("USE_BLOWFISH")
	use_qsee, _ := ctx.AConfig().GetMkVar("SEC_AP_CHIP_VENDOR")
	tz_name := ""

	if use_kinibi == "true" {
		fmt.Println("[PROCA] USE_MOBICORE")
		tz_name = "tbase"
		p.Cflags = []string{"-DTBASE"}
	} else if use_teegris == "true" {
		fmt.Println("[PROCA] USE_BLOWFISH")
		tz_name = "teegris"
		p.Cflags = []string{"-DTEEGRIS"}
	} else if use_qsee == "qcom" {
		fmt.Println("[PROCA] USE_QSEE")
		tz_name = "qsee"
		p.Cflags = []string{"-DQSEE"}
	}
	p.Cflags = append(p.Cflags,	"-DVERSION_NAME=\"PA_NWD_lib,"+tz_name+"\"")
	ctx.AppendProperties(p)
}

func procadaemonDefaults(ctx android.LoadHookContext) {
	type props struct {
		Cflags      []string
		Init_rc     []string
	}

	p := &props{}

	proca_enabled, ok := ctx.AConfig().Getspf("SEC_PRODUCT_FEATURE_SECURITY_SUPPORT_PROCA")
	sssi_target, _ := ctx.AConfig().Getspf("SEC_PRODUCT_FEATURE_BUILD_SSSI_TARGET")

	if !ok || proca_enabled != "TRUE" || sssi_target == "system" {
		fmt.Println("[PROCA] PROCA is disabled. Daemon should not be built.")
	}

	use_kinibi, _ := ctx.AConfig().GetMkVar("USE_MOBICORE")
	use_teegris, _ := ctx.AConfig().GetMkVar("USE_BLOWFISH")
	use_qsee, _ := ctx.AConfig().GetMkVar("SEC_AP_CHIP_VENDOR")
	tz_name := ""

	if use_kinibi == "true" {
		fmt.Println("[PROCA] USE_MOBICORE")
		tz_name = "tbase"
		p.Init_rc = []string{"rc/pa_daemon_kinibi.rc"}
		p.Cflags = []string{"-DTBASE"}
	} else if use_teegris == "true" {
		fmt.Println("[PROCA] USE_BLOWFISH")
		tz_name = "teegris"
		p.Init_rc = []string{"rc/pa_daemon_teegris.rc"}
		p.Cflags = []string{"-DTEEGRIS"}
	} else if use_qsee == "qcom" {
		fmt.Println("[PROCA] USE_QSEE")
		tz_name = "qsee"
		p.Init_rc = []string{"rc/pa_daemon_qsee.rc"}
		p.Cflags = []string{"-DQSEE"}
	}
	p.Cflags = append(p.Cflags,	"-DVERSION_NAME=\"PA_Daemon,"+tz_name+"\"")
	ctx.AppendProperties(p)
}

func procaplatformDefaults(ctx android.LoadHookContext) {
	type platformDefaultsProperties struct {
		Shared_libs []string
		Static_libs []string
	}

	p := &platformDefaultsProperties{}

	use_kinibi, _ := ctx.AConfig().GetMkVar("USE_MOBICORE")
	use_teegris, _ := ctx.AConfig().GetMkVar("USE_BLOWFISH")
	use_qsee, _ := ctx.AConfig().GetMkVar("SEC_AP_CHIP_VENDOR")
	sssi_target, _ := ctx.AConfig().Getspf("SEC_PRODUCT_FEATURE_BUILD_SSSI_TARGET")
	if use_kinibi == "true" {
		fmt.Println("[PROCA platform] USE_MOBICORE")
		p.Shared_libs = append(p.Shared_libs, "libMcClient")
		p.Static_libs = append(p.Static_libs, "libmultibuild_libteecl_tbase")
	} else if use_teegris == "true" {
		fmt.Println("[PROCA platform] USE_BLOWFISH")
		p.Shared_libs = append(p.Shared_libs, "libteecl")
	} else if use_qsee == "qcom" {
		if sssi_target != "system" {
			fmt.Println("[PROCA platform] USE_QSEE")
			p.Shared_libs = append(p.Shared_libs, "libQSEEComAPI")
			p.Static_libs = append(p.Static_libs, "libmultibuild_libteecl_qsee")
		} else {
			fmt.Println("[PROCA platform] libQSEECOMAPI_system is not needed")
		}
	}
	ctx.AppendProperties(p)
}

func init() {
	android.RegisterModuleType("proca_daemon_defaults_sources", daemonDefaultsFactory)
	android.RegisterModuleType("proca_lib_defaults_sources", libDefaultsFactory)
	android.RegisterModuleType("proca_platform_defaults_sources", platformDefaultsFactory)
}

func libDefaultsFactory() android.Module {
	module := cc.DefaultsFactory()
	android.AddLoadHook(module, procalibDefaults)
	return module
}

func daemonDefaultsFactory() android.Module {
	module := cc.DefaultsFactory()
	android.AddLoadHook(module, procadaemonDefaults)
	return module
}

func platformDefaultsFactory() android.Module {
	module := cc.DefaultsFactory()
	android.AddLoadHook(module, procaplatformDefaults)
	return module
}
