/*
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"
	//"io/ioutil"

	"encoding/json"
	"fmt"
	input "github.com/charmbracelet/bubbles/textinput"
	tea "github.com/charmbracelet/bubbletea"
	te "github.com/muesli/termenv"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"io/ioutil"
	"log"
	"pgm/data"
	"pgm/logger"

	//"github.com/spf13/viper"
	//"log"
	//"pgm/data"
	//"pgm/logger"

	"os"
)

const focusedTextColor = "205"

var (
	color               = te.ColorProfile().Color
	focusedPrompt       = te.String("> ").Foreground(color("205")).String()
	blurredPrompt       = "> "
	focusedSubmitButton = "[ " + te.String("Submit").Foreground(color("205")).String() + " ]"
	blurredSubmitButton = "[ " + te.String("Submit").Foreground(color("240")).String() + " ]"
)


// addCmd represents the add command
var addCmd = &cobra.Command{
	Use:   "add",
	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) {
		// This is where we'll listen for the choice the user makes in the Bubble
		// Tea program.
		var cfg []string
		result := make(chan []string, 1)

		if err := tea.NewProgram(initialModel(result)).Start(); err != nil {
			fmt.Printf("could not start program: %s\n", err)
			os.Exit(1)
		}
		// Print out the final choice.
		if cfg = <-result; len(cfg) !=0 {
			configWriter(cfg)
		}




	},
}

func init() {
	rootCmd.AddCommand(addCmd)

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

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

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


type hostCfgFile struct {
	index         int
	fn            input.Model
	hostname      input.Model
	dbname        input.Model
	username      input.Model
	secret		  input.Model
	submitButton  string
	Filename      chan []string
}

func initialModel(Filename chan []string ) hostCfgFile {
	fn := input.NewModel()
	fn.Placeholder = "filename (no spaces)"
	fn.Focus()
	fn.Prompt = focusedPrompt
	fn.TextColor = focusedTextColor
	fn.CharLimit = 32

	hn := input.NewModel()
	hn.Placeholder = "hostname"
	hn.Prompt = blurredPrompt
	hn.CharLimit = 64

	dbn := input.NewModel()
	dbn.Placeholder = "dbname"
	dbn.Prompt = blurredPrompt
	dbn.CharLimit = 64

	un := input.NewModel()
	un.Placeholder = "username"
	un.Prompt = blurredPrompt
	un.CharLimit = 30


	secret := input.NewModel()
	secret.Placeholder = "secret"
	secret.Prompt = blurredPrompt
	secret.CharLimit = 99

	return hostCfgFile{0, fn, hn, dbn, un, secret,blurredSubmitButton, Filename}

}
func (m hostCfgFile) Init() tea.Cmd {
	return tea.Batch(
		input.Blink(m.fn),
		input.Blink(m.hostname),
		input.Blink(m.dbname),
		input.Blink(m.username),
		input.Blink(m.secret),
	)
}

func (m hostCfgFile) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	var cmd tea.Cmd

	switch msg := msg.(type) {
	case tea.KeyMsg:
		switch msg.String() {

		case "ctrl+c", "esc":
			close(m.Filename)
			return m, tea.Quit

		// Cycle between inputs
		case "tab", "shift+tab", "enter", "up", "down":

			inputs := []input.Model{
				m.fn,
				m.hostname,
				m.dbname,
				m.username,
				m.secret,
			}

			s := msg.String()

			// Did the user press enter while the submit button was focused?
			// If so, exit.
			if s == "enter" && m.index == len(inputs) {
				var cfg []string
				for _, i := range inputs{
					cfg = append(cfg, i.Value())
				}
				m.Filename <- cfg
				return m, tea.Quit
			}

			// Cycle indexes
			if s == "up" || s == "shift+tab" {
				m.index--
			} else {
				m.index++
			}

			if m.index > len(inputs) {
				m.index = 0
			} else if m.index < 0 {
				m.index = len(inputs)
			}

			for i := 0; i <= len(inputs)-1; i++ {
				if i == m.index {
					// Set focused state
					inputs[i].Focus()
					inputs[i].Prompt = focusedPrompt
					inputs[i].TextColor = focusedTextColor
					continue
				}
				// Remove focused state
				inputs[i].Blur()
				inputs[i].Prompt = blurredPrompt
				inputs[i].TextColor = ""
			}

			m.fn = inputs[0]
			m.hostname = inputs[1]
			m.dbname = inputs[2]
			m.username = inputs[3]
			m.secret = inputs[4]

			if m.index == len(inputs) {
				m.submitButton = focusedSubmitButton
			} else {
				m.submitButton = blurredSubmitButton
			}

			return m, nil
		}
	}

	// Handle character input and blinks
	m, cmd = updateInputs(msg, m)
	return m, cmd
}

// Pass messages and models through to text input components. Only text inputs
// with Focus() set will respond, so it's safe to simply update all of them
// here without any further logic.
func updateInputs(msg tea.Msg, m hostCfgFile) (hostCfgFile, tea.Cmd) {
	var (
		cmd  tea.Cmd
		cmds []tea.Cmd
	)

	m.fn, cmd = input.Update(msg, m.fn)
	cmds = append(cmds, cmd)

	m.hostname, cmd = input.Update(msg, m.hostname)
	cmds = append(cmds, cmd)

	m.dbname, cmd = input.Update(msg, m.dbname)
	cmds = append(cmds, cmd)

	m.username, cmd = input.Update(msg, m.username)
	cmds = append(cmds, cmd)

	m.secret, cmd = input.Update(msg, m.secret)
	cmds = append(cmds, cmd)


	return m, tea.Batch(cmds...)
}

func (m hostCfgFile) View() string {
	s := "\n"

	inputs := []string{
		input.View(m.fn),
		input.View(m.hostname),
		input.View(m.dbname),
		input.View(m.username),
		input.View(m.secret),
	}

	for i := 0; i < len(inputs); i++ {
		s += inputs[i]
		if i < len(inputs)-1 {
			s += "\n"
		}
	}

	s += "\n\n" + m.submitButton + "\n"
	return s
}

//
//// Writes configs to .pgm/hosts directory
func configWriter(s []string){
	h := viper.GetString("hostsDir")
	var c data.HostDetails
	var fp string
	var cfg []byte
	var isEmpty bool
	isEmpty = checkEmpty(s)
	if isEmpty == true {
		logger.Logger("[LOG] one or more fields was empty when writing file: " + s[0] )
		fmt.Println("one or more fields was empty when submitted, please try again")
		os.Exit(0)
	}
	c = data.HostDetails{s[4], s[1], s[2], s[3]}
	cfgFileName := s[0]
	if _, err := os.Stat(h + "/" + cfgFileName); os.IsNotExist(err) {
		fp = h + "/" + cfgFileName
	} else {
		logger.Logger("[LOG] duplicate file name found: " + cfgFileName)
		os.Exit(1)
	}
	_ = cfg
	f, err := os.Create(fp)
	if err != nil {
		log.Fatal("unable to create file: " + fp)
	}
	defer f.Close()
	if err != nil {
		log.Fatal("error writing to file: " + err.Error())
	}
	cfg, err = json.MarshalIndent(c, "", " ")
	if err != nil {
		log.Fatal(err)
	}
	logger.Logger("[LOG] writing file to: " + fp)
	_ = ioutil.WriteFile(fp, cfg, 0644)
	lgd := logger.Logger("[LOG] configuration file written: dbname::" + c.DatabaseName + "::hostname::" + c.Hostname)
	if lgd != true {
		fmt.Println("failed to log")
	}
}

// looks through user inputs for  zero length strings
// if any one is empty we do not accept the input and exit.
func checkEmpty(s []string ) bool {
	for _, ip := range s {
		if len(ip) < 1 {
			return true
		} else {
			continue
		}
	}
	return false
}