package main

import (
	"context"
	"encoding/json"
	"net/http"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/client"
	"imuslab.com/zoraxy/mod/utils"
)

// HandleDockerAvaible check if teh docker related functions should be avaible in front-end
func HandleDockerAvailable(w http.ResponseWriter, r *http.Request) {
	js, _ := json.Marshal(*runningInDocker)
	utils.SendJSONResponse(w, string(js))
}

// handleDockerContainersList return the current list of docker containers
// currently listening to the same bridge network interface. See PR #202 for details.
func HandleDockerContainersList(w http.ResponseWriter, r *http.Request) {
	apiClient, err := client.NewClientWithOpts(client.WithVersion("1.43"))
	if err != nil {
		SystemWideLogger.PrintAndLog("Docker", "Unable to create new docker client", err)
		utils.SendErrorResponse(w, "Docker client initiation failed")
		return
	}
	defer apiClient.Close()

	containers, err := apiClient.ContainerList(context.Background(), container.ListOptions{All: true})
	if err != nil {
		SystemWideLogger.PrintAndLog("Docker", "List docker container failed", err)
		utils.SendErrorResponse(w, "List docker container failed")
		return
	}

	networks, err := apiClient.NetworkList(context.Background(), types.NetworkListOptions{})
	if err != nil {
		SystemWideLogger.PrintAndLog("Docker", "List docker network failed", err)
		utils.SendErrorResponse(w, "List docker network failed")
		return
	}

	result := map[string]interface{}{
		"network":    networks,
		"containers": containers,
	}

	js, err := json.Marshal(result)
	if err != nil {
		utils.SendErrorResponse(w, err.Error())
		return
	}

	utils.SendJSONResponse(w, string(js))
}
