package file

import (
	"net/http"
	"path/filepath"

	"be.ems/lib/config"
	"be.ems/lib/log"
	"be.ems/lib/services"
	"github.com/gorilla/mux"
)

var (
	// parameter config management
	UriFile       = config.DefaultUriPrefix + "/fileManagement/{apiVersion}/{location}/file"
	CustomUriFile = config.UriPrefix + "/fileManagement/{apiVersion}/{location}/file"
)

// func init() {
// 	routes.Register("POST", UriFile, UploadFile, nil)
// 	routes.Register("GET", UriFile, DownloadFile, nil)
// 	routes.Register("DELETE", UriFile, DeleteFile, nil)
// }

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

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

	vars := mux.Vars(r)
	location := vars["location"]
	if location == "" {
		log.Error("location is empty")
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}
	params := r.URL.Query()
	rename, ok := params["rename"]
	if !ok {
		log.Info("rename parameter is not exist")
	}
	log.Debug("rename:", rename)

	var path, fileName string
	if len(rename) > 0 {
		fileName = rename[0]
	}
	if location == "upload" {
		path = config.GetYamlConfig().OMC.Upload
		fileNamePath, err := services.HandleUploadFile(r, path, fileName)
		if err != nil {
			log.Error("Faile to HandleUploadFile:", err)
			services.ResponseInternalServerError500ProcessError(w, err)
			return
		}
		fileName = fileNamePath
	} else {
		path = config.GetYamlConfig().OMC.FrontUpload
		fileNamePath, err := services.HandleUploadFile(r, path, fileName)
		if err != nil {
			log.Error("Faile to HandleUploadFile:", err)
			services.ResponseInternalServerError500ProcessError(w, err)
			return
		}
		fileName = fileNamePath
	}
	log.Debugf("upload file=%s to path=%s", fileName, path)

	services.ResponseStatusOK204NoContent(w)
	return
}

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

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

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

	// 全路径文件下载
	filePath := r.URL.Query().Get("path")
	if location == "path" && filePath != "" {
		// 截取文件路径
		dir := filepath.Dir(filePath)
		// 截取文件名
		fileName := filepath.Base(filePath)
		services.ResponseFileWithNameAndMD5(w, http.StatusOK, fileName, dir, "")
		return
	}

	params := r.URL.Query()
	fileNames, ok := params["loc"]
	if !ok {
		log.Info("loc parameter is not exist")
	}
	var path string
	if location == "upload" {
		path = config.GetYamlConfig().OMC.Upload
	} else {
		path = config.GetYamlConfig().OMC.FrontUpload
	}
	for _, fileName := range fileNames {
		services.ResponseFileWithNameAndMD5(w, http.StatusOK, fileName, path, "")
	}
	return
}

func DeleteFile(w http.ResponseWriter, r *http.Request) {
	log.Debug("DeleteFile 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
	}

	services.ResponseStatusOK204NoContent(w)
	return
}
