/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
	"encoding/json"
	"fmt"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/muesli/termenv"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"io/ioutil"
	"log"
	"os"
	"shiny-pancake/internal/database"
	"shiny-pancake/internal/gui"
	"shiny-pancake/internal/model"
	"shiny-pancake/logger"
	"strconv"
	"strings"
	"time"

)

type host struct {
	tic      int
	t        string
	choices  []string
	choice   int
	chosen   bool
	Quitting bool
	Frames   int
	File     chan string


}

var (
	term   = termenv.ColorProfile()
	subtle = makeFgStyle("241")
	dot    = colorFg(" • ", "236")
)


// billyCmd represents the billy command
var loadCmd = &cobra.Command{
	Use:   "load",
	Short: "A brief description of your command",
	Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
	Run: func(cmd *cobra.Command, args []string) {
		var title string
		var pgHosts []string
		var fn string

		result := make(chan string, 1)

		pgHosts = readHosts()
		title = "choose db to load, \"l\" or enter will load your choice"
		var hostChoice host
		hostChoice = host{60, title, pgHosts, 0, false, false, 0, result }
		if err := tea.NewProgram(hostChoice).Start(); err != nil {
			os.Exit(1)
		}

		// Print out the final choice.
		if fn = <-result; len(fn) !=0 {
			var hostDir string
			var db *database.Database
			hostDir = viper.GetString("hostsDirectory")
			file, _ := ioutil.ReadFile(hostDir + "/"+ fn)
			data := model.HostDetails{}
			err := json.Unmarshal(file, &data)
			if err != nil {
				var l logger.Log
				l = logger.Log{"error", logrus.Fields{"loadFailed": fn }, "config not loaded"}
				logger.Lgr(&l)
				fmt.Println("invalid config file. ")
				os.Exit(1)
			}
			var l logger.Log
			l = logger.Log{"info", logrus.Fields{"loaded": fn }, "config loading to ui"}
			logger.Lgr(&l)
			db = database.New(&data)

			// Initialise the Gui / Tui
			gui := gui.New(db)
			if err := gui.Start(); err != nil {
				log.Fatalf("main: Cannot start tui: %s", err)
			}
		}

	},
}


func init() {
	rootCmd.AddCommand(loadCmd)

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// billyCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// billyCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}



func readHosts() []string {
	var hosts []string
	var hostsDir string
	hostsDir = viper.GetString("hostsDirectory")
	file, err := os.Open(hostsDir)
	if err != nil {
		var l logger.Log
		l = logger.Log{"error", logrus.Fields{"filepath": hostsDir}, "unable to access directory"}
		logger.Lgr(&l)
		os.Exit(0)

	}
	defer file.Close()

	hosts, err = file.Readdirnames(0) // 0 to read all files and folders
	if err != nil {
		log.Fatal(err)
	}
	return hosts
}



func (m host) Init() tea.Cmd {
	return tick()
}

func choicesUpdate(msg tea.Msg, m host) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {

	case tea.KeyMsg:
		switch msg.String() {
		case "q", "esc":
			m.Quitting = true
			return m, tea.Quit
		case "j", "down":
			m.choice += 1
			if m.choice > len(m.choices) {
				m.choice = len(m.choices)
			}
		case "k", "up":
			m.choice -= 1
			if m.choice < 0 {
				m.choice = 0
			}
		case "l", "enter":
			m.File <- m.choices[m.choice]
			return m, tea.Quit
		}

	case tickMsg:
		if m.tic == 0 {
			m.Quitting = true
			return m, tea.Quit
		}
		m.tic -= 1
		return m, tick()
	}
	return m, nil
}

// Update loop for the second view after a choice has been made
func (m host) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	if msg, ok := msg.(tea.KeyMsg); ok {
		k := msg.String()
		if k == "q" || k == "esc" || k == "ctrl+c" {
			m.Quitting = true
			return m, tea.Quit
		}
	}

	if !m.chosen {
		return choicesUpdate(msg, m)
	}
	return m, tea.Quit
}

// Views return a string based on data in the host. That string which will be
// rendered to the terminal.
func (m host) View() string {
	var s string
	if m.Quitting {
		return "leaving program..."

	}
	if !m.chosen {
		s = choicesView(m)
	} else {
		m.Quitting = true
		return "leaving program..."
	}
	return s
}



// Creates the menu for choosing a file from your configs.
// Exits after 60 seconds.
// Gets Title from outside in the load method if this... should ever be needed elsewhere.
func choicesView(m host) string {
	var menu string
	c := m.choice

	tpl := m.t + "\n\n"
	tpl += "%s\n\n"
	tpl += "menu exits in %s seconds\n\n"
	tpl += subtle("j/k, up/down: select") + dot + subtle("enter: choose") + dot + subtle("q, esc: quit")
	for i, s := range m.choices {
		menu = menu + "::" + checkbox(s, c == i)
	}
	mn := strings.Replace(menu, "::", "\n", -1)
	choices := fmt.Sprintf(
		mn,
	)

	return fmt.Sprintf(tpl, choices, colorFg(strconv.Itoa(m.tic), "79"))
}

// the rest of these functions are to do with looks and aesthetics

// returns a neat checkbox
func checkbox(label string, checked bool) string {
	if checked {
		return colorFg("[x] "+label, "212")
	}
	return fmt.Sprintf("[ ] %s", label)
}

// fun colors
func colorFg(val, color string) string {
	return termenv.String(val).Foreground(term.Color(color)).String()
}

// Return a function that will colorize the foreground of a given string.
func makeFgStyle(color string) func(string) string {
	return termenv.Style{}.Foreground(term.Color(color)).Styled
}

// Frame event
func Frame() tea.Cmd {
	return tea.Tick(time.Second/60, func(time.Time) tea.Msg {
		return FrameMsg{}
	})
}

type tickMsg struct{}
type FrameMsg struct{}

// exits if no choice is selected in a minute
func tick() tea.Cmd {
	return tea.Tick(time.Second, func(time.Time) tea.Msg {
		return tickMsg{}
	})
}
