package cm

import (
	"net/http"

	"ems.agt/lib/log"
	"ems.agt/lib/services"
	"ems.agt/restagent/config"

	"github.com/gorilla/mux"
)

var (
	// License
	LicenseUri   = config.DefaultUriPrefix + "/systemManagement/{apiVersion}/{neType}/license"
	NeLicenseUri = config.DefaultUriPrefix + "/systemManagement/{apiVersion}/{neType}/license/{neId}"

	CustomLicenseUri   = config.UriPrefix + "/systemManagement/{apiVersion}/{neType}/license"
	CustomNeLicenseUri = config.UriPrefix + "/systemManagement/{apiVersion}/{neType}/license/{neId}"
)

func UploadLicenseFile(w http.ResponseWriter, r *http.Request) {
	log.Debug("UploadLicenseFile processing... ")

	_, err := services.CheckFrontValidRequest(w, r)
	if err != nil {
		log.Error("Http request error:", err)
		return
	}

	vars := mux.Vars(r)
	neType := vars["neType"]
	if neType == "" {
		log.Error("neType is empty")
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}
	// neTypeUpper := strings.ToUpper(neType)
	// neTypeLower := strings.ToLower(neType)

	services.ResponseStatusOK204NoContent(w)
	return
}

func DownloadLicenseFile(w http.ResponseWriter, r *http.Request) {
	log.Debug("DownloadLicenseFile processing... ")

	_, err := services.CheckFrontValidRequest(w, r)
	if err != nil {
		log.Error("Request error:", err)
		return
	}

	vars := mux.Vars(r)
	neType := vars["neType"]
	if neType == "" {
		log.Error("neType is empty")
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}
	// // neTypeUpper := strings.ToUpper(neType)
	// //neTypeLower := strings.ToLower(neType)

	// version := vars["version"]
	// if version == "" {
	// 	log.Error("version is empty")
	// 	services.ResponseNotFound404UriNotExist(w, r)
	// 	return
	// }

	// sql := fmt.Sprintf("select * from ne_software where ne_type='%s' and version='%s'", neTypeUpper, version)
	// neSoftware, err := dborm.XormGetDataBySQL(sql)
	// if err != nil {
	// 	log.Error("Faile to XormGetDataBySQL:", err)
	// 	services.ResponseInternalServerError500ProcessError(w, err)
	// 	return
	// } else if len(*neSoftware) == 0 {
	// 	err := global.ErrCMNotFoundTargetSoftware
	// 	log.Error(err)
	// 	services.ResponseInternalServerError500ProcessError(w, err)
	// 	return
	// }

	// fileName := (*neSoftware)[0]["file_name"]
	// path := (*neSoftware)[0]["path"]
	// md5Sum := (*neSoftware)[0]["md5_sum"]

	// services.ResponseFileWithNameAndMD5(w, http.StatusOK, fileName, path, md5Sum)
	return
}

func DeleteLcenseFile(w http.ResponseWriter, r *http.Request) {
	log.Debug("DeleteLcenseFile processing... ")

	_, err := services.CheckFrontValidRequest(w, r)
	if err != nil {
		log.Error("Request error:", err)
		return
	}

	vars := mux.Vars(r)
	neType := vars["neType"]
	if neType == "" {
		log.Error("neType is empty")
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}
	// neTypeUpper := strings.ToUpper(neType)
	// //neTypeLower := strings.ToLower(neType)

	// version := vars["version"]
	// if version == "" {
	// 	log.Error("version is empty")
	// 	services.ResponseNotFound404UriNotExist(w, r)
	// 	return
	// }

	// sql := fmt.Sprintf("select * from ne_software where ne_type='%s' and version='%s'", neTypeUpper, version)
	// neSoftware, err := dborm.XormGetDataBySQL(sql)
	// if err != nil {
	// 	log.Error("Faile to XormGetDataBySQL:", err)
	// 	services.ResponseInternalServerError500ProcessError(w, err)
	// 	return
	// } else if len(*neSoftware) == 0 {
	// 	err := global.ErrCMNotFoundTargetSoftware
	// 	log.Error(err)
	// 	services.ResponseInternalServerError500ProcessError(w, err)
	// 	return
	// }

	// where := fmt.Sprintf("ne_type='%s' and version='%s'", neTypeUpper, version)
	// affected, err := dborm.XormDeleteDataByWhere(where, "ne_software")
	// if err != nil || affected == 0 {
	// 	log.Error("Faile to XormGetDataBySQL:", err)
	// 	services.ResponseInternalServerError500ProcessError(w, err)
	// 	return
	// }

	// fileName := (*neSoftware)[0]["file_name"]
	// path := (*neSoftware)[0]["path"]
	// filePath := fmt.Sprintf("%s/%s", path, fileName)
	// err = os.Remove(filePath)
	// if err != nil {
	// 	log.Error("Faile to Remove:", err)
	// 	services.ResponseInternalServerError500ProcessError(w, err)
	// 	return
	// }

	services.ResponseStatusOK204NoContent(w)
	return
}
