package cmd

import (
	"context"
	"fmt"
	"github.com/up9inc/mizu/cli/kubernetes"
	"github.com/up9inc/mizu/cli/mizu"
	"net/http"
)

func runMizuView() {
	kubernetesProvider, err := kubernetes.NewProvider(mizu.Config.View.KubeConfigPath)
	if err != nil {
		mizu.Log.Error(err)
		return
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	exists, err := kubernetesProvider.DoesServicesExist(ctx, mizu.Config.MizuResourcesNamespace, mizu.ApiServerPodName)
	if err != nil {
		panic(err)
	}
	if !exists {
		mizu.Log.Infof("The %s service not found", mizu.ApiServerPodName)
		return
	}

	mizuProxiedUrl := kubernetes.GetMizuApiServerProxiedHostAndPath(mizu.Config.View.GuiPort)
	_, err = http.Get(fmt.Sprintf("http://%s/", mizuProxiedUrl))
	if err == nil {
		mizu.Log.Infof("Found a running service %s and open port %d", mizu.ApiServerPodName, mizu.Config.View.GuiPort)
		return
	}
	mizu.Log.Infof("Found service %s, creating k8s proxy", mizu.ApiServerPodName)

	mizu.Log.Infof("Mizu is available at  http://%s\n", kubernetes.GetMizuApiServerProxiedHostAndPath(mizu.Config.View.GuiPort))
	err = kubernetes.StartProxy(kubernetesProvider, mizu.Config.View.GuiPort, mizu.Config.MizuResourcesNamespace, mizu.ApiServerPodName)
	if err != nil {
		mizu.Log.Errorf("Error occurred while running k8s proxy %v", err)
	}
}
