package cmii_operator

import (
	"errors"
	"os"
	"wdd.io/agent-go/executor"
	"wdd.io/agent-go/utils"
)

const OfflineImageGzipFolderPrefix = "/root/octopus_image/"
const OfflineDeployHarborHost = "harbor.wdd.io"

func FetchDemoImages(projectName string, gzipSplit bool) (errorPullImageList, errorGzipImageList []string) {

	// generate a project folder
	err := os.Mkdir(OfflineImageGzipFolderPrefix+projectName, os.ModeDir)
	if err != nil {
		if !errors.Is(err, os.ErrExist) {
			log.ErrorF("[FetchDemoImages] - create folder of %s error %s", OfflineImageGzipFolderPrefix+projectName, err.Error())
			return errorPullImageList, errorGzipImageList
		}
	}

	// get demo image version map
	backendMap, frontendMap := BackupAllCmiiDeploymentToMap(demo)

	utils.BeautifulPrint(backendMap)
	utils.BeautifulPrint(frontendMap)

	// save map to file
	backendMapFile := OfflineImageGzipFolderPrefix + projectName + "-backend-app.json"
	frontendMapFile := OfflineImageGzipFolderPrefix + projectName + "-frontend-app.json"
	_ = os.Remove(backendMapFile)
	_ = os.Remove(frontendMapFile)

	executor.BasicAppendContentToFile(
		utils.BeautifulPrintToString(backendMap),
		backendMapFile,
	)
	executor.BasicAppendContentToFile(
		utils.BeautifulPrintToString(frontendMap),
		frontendMapFile,
	)

	// download image
	backendPull := ImagePullFromCmiiHarborByMap(backendMap, true)
	frontendPull := ImagePullFromCmiiHarborByMap(frontendMap, true)

	// compress image
	if gzipSplit {
		for image, tag := range backendMap {
			if !ImageSaveToTarGZ(image+":"+tag, OfflineImageGzipFolderPrefix+projectName+"/app/") {
				errorGzipImageList = append(errorGzipImageList, CmiiHarborPrefix+image+":"+tag)
			}
		}
		for image, tag := range frontendMap {
			if !ImageSaveToTarGZ(image+":"+tag, OfflineImageGzipFolderPrefix+projectName+"/app/") {
				errorGzipImageList = append(errorGzipImageList, CmiiHarborPrefix+image+":"+tag)
			}
		}
	}
	// upload to harbor

	// clean up images

	errorPullImageList = append(errorPullImageList, backendPull...)
	errorPullImageList = append(errorPullImageList, frontendPull...)

	return errorPullImageList, errorGzipImageList
}

func FetchDependencyRepos(gzipSplit bool) (errorPullImageList, errorGzipImageList []string) {
	err := os.Mkdir(OfflineImageGzipFolderPrefix, os.ModeDir)
	if err != nil {
		if !errors.Is(err, os.ErrExist) {
			log.ErrorF("[FetchDependencyRepos] - create folder of %s error %s", OfflineImageGzipFolderPrefix, err.Error())
		}
	}

	errorPullImageList, errorGzipImageList = ImagePullFromListAndCompressSplit(MiddlewareAmd64, OfflineImageGzipFolderPrefix+"middle/")

	pull, gzipImageList := ImagePullFromListAndCompressSplit(Rancher1204Amd64, OfflineImageGzipFolderPrefix+"rke/")

	return append(errorPullImageList, pull...), append(errorGzipImageList, gzipImageList...)

}

func LoadSplitGzipImageToTargetHarbor(projectName, targetHarborHost string) (errorLoadImageNameList, errorPushImageNameList []string) {

	// list folder
	projectGzipFolder := OfflineImageGzipFolderPrefix + projectName
	errorLoadImageNameList = append(errorLoadImageNameList, ImageLoadFromFolderPath(projectGzipFolder)...)
	// read from json
	errorPushImageNameList = append(errorPushImageNameList, ImageTagFromListAndPushToCHarbor(Cmii520DemoImageList, targetHarborHost)...)

	// re-tag
	// push

	// todo clean host and harbor
	// check harbor exits

	return errorLoadImageNameList, errorPushImageNameList
}

func LoadSplitDepGzipImageToTargetHarbor(targetHarborHost string) (errorLoadImageNameList []string, errorPushImageNameList []string) {

	middle := OfflineImageGzipFolderPrefix + "middle/"
	rke := OfflineImageGzipFolderPrefix + "rke/"
	errorLoadImageNameList = append(errorLoadImageNameList, ImageLoadFromFolderPath(middle)...)
	errorLoadImageNameList = append(errorLoadImageNameList, ImageLoadFromFolderPath(rke)...)

	errorPushImageNameList = append(errorPushImageNameList, ImageTagFromListAndPushToCHarbor(MiddlewareAmd64, targetHarborHost)...)
	errorPushImageNameList = append(errorPushImageNameList, ImageTagFromListAndPushToCHarbor(Rancher1204Amd64, targetHarborHost)...)

	return errorLoadImageNameList, errorPushImageNameList

}
