/*
Copyright © Brian Buller <brian@bullercodeworks.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package widgets

import (
	"fmt"
	"time"

	wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
	"github.com/gdamore/tcell"
)

// Chat is a greedy widget and will consume all of the space you give it.
type Chat struct {
	id    string
	style tcell.Style

	x, y     int
	w, h     int
	active   bool
	visible  bool
	tabbable bool

	title       string
	rawLog      []string
	log         []*ChatMsg
	logPosition int

	value  string
	cursor int

	history         []ChatMsg
	historyPosition int

	keyMap KeyMap
}

var _ Widget = (*Chat)(nil)

func NewChat(id string, s tcell.Style) *Chat {
	ret := &Chat{}
	ret.Init(id, s)
	return ret
}

func (w *Chat) Init(id string, s tcell.Style) {
	w.id, w.style = id, s
	w.visible = true
	w.initKeyMap()
	w.tabbable = true
}

func (w *Chat) Id() string { return w.id }
func (w *Chat) HandleResize(ev *tcell.EventResize) {
	w.w, w.h = ev.Size()
	// w.w = wh.Min(w.w, w.WantW())
	// w.h = wh.Min(w.h, w.WantH())
}

func (w *Chat) HandleKey(ev *tcell.EventKey) bool {
	if !w.active {
		return false
	}
	if wh.IsKey(*ev, tcell.KeyEsc) {
		w.SetActive(false)
		return true
	}
	if wh.IsBS(*ev) {
		if w.cursor > 0 {
			w.cursor--
		}
		if w.cursor < len(w.value) {
			w.value = w.value[:w.cursor] + w.value[w.cursor+1:]
		}
	}
	if ok := w.keyMap.Handle(ev); ok {
		return true
	}

	var ch string
	if wh.KeyIsDisplayable(*ev) {
		ch = string(ev.Rune())
		if w.cursor < len(w.value) {
			strPt1 := w.value[:w.cursor]
			strPt2 := w.value[w.cursor:]
			w.value = fmt.Sprintf("%s%s%s", strPt1, ch, strPt2)
		} else {
			w.value = fmt.Sprintf("%s%s", w.value, ch)
		}
		w.cursor++
		return true
	}
	return false
}
func (w *Chat) HandleTime(ev *tcell.EventTime) {}

func (w *Chat) Draw(screen tcell.Screen) {
	if !w.visible {
		return
	}
	dStyle := w.style
	if !w.active {
		dStyle = dStyle.Dim(true)
	}
	if w.title != "" {
		wh.TitledBorderFilled(w.x, w.y, w.x+w.w-1, w.y+w.h, w.title, wh.BRD_SIMPLE, dStyle, screen)
	} else {
		wh.BorderFilled(w.x, w.y, w.x+w.w-1, w.y+w.h, wh.BRD_SIMPLE, dStyle, screen)
	}

	x, y := w.x+1, w.y+1+w.h-3
	for i := 0; i < w.h-2; i++ {
		if len(w.log) > i {
			line := w.log[len(w.log)-i-1].String()
			if len(line) > w.w-2 {
				line = line[:w.w-2]
			}
			wh.DrawText(x, y, wh.PadR(line, w.w), dStyle, screen)
			y--
		}
	}
	y = w.y + w.h - 1
	cursor := " "
	pre := "> "
	var post string
	if len(w.value) > 0 {
		pre = fmt.Sprintf("%s%s", pre, w.value[:w.cursor])
		if w.cursor < len(w.value) {
			cursor = string(w.value[w.cursor])
			post = w.value[w.cursor+1:]
		}
	}
	wh.DrawText(x, y, pre, dStyle, screen)
	x += len(pre)
	wh.DrawText(x, y, cursor, dStyle.Reverse(w.active), screen)
	x += 1
	wh.DrawText(x, y, wh.PadR(post, w.w-x), dStyle, screen)
	// x += len(post) - 1
}

func (w *Chat) Active() bool       { return w.active }
func (w *Chat) SetActive(a bool)   { w.active = a }
func (w *Chat) Visible() bool      { return w.visible }
func (w *Chat) SetVisible(a bool)  { w.visible = a }
func (w *Chat) Focusable() bool    { return true }
func (w *Chat) SetTabbable(b bool) { w.tabbable = b }
func (w *Chat) Tabbable() bool     { return w.tabbable }
func (w *Chat) SetX(x int)         { w.x = x }
func (w *Chat) SetY(y int)         { w.y = y }
func (w *Chat) GetX() int          { return w.x }
func (w *Chat) GetY() int          { return w.y }
func (w *Chat) GetPos() Coord      { return Coord{X: w.x, Y: w.y} }
func (w *Chat) SetPos(c Coord)     { w.x, w.y = c.X, c.Y }
func (w *Chat) GetW() int          { return w.w }
func (w *Chat) GetH() int          { return w.h }
func (w *Chat) SetW(wd int)        { w.w = wd }
func (w *Chat) SetH(h int)         { w.h = h }
func (w *Chat) SetSize(c Coord)    { w.w, w.h = c.X, c.Y }
func (w *Chat) WantW() int         { return wh.MaxInt }
func (w *Chat) WantH() int         { return wh.MaxInt }
func (w *Chat) MinW() int          { return 2 + 20 }
func (w *Chat) MinH() int          { return 6 }

func (w *Chat) initKeyMap() {
	w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
		tcell.KeyEsc: func(ev *tcell.EventKey) bool {
			w.SetActive(false)
			return true
		},
		tcell.KeyUp: func(ev *tcell.EventKey) bool {
			if w.historyPosition < len(w.history)-1 {
				w.historyPosition++
				w.value = w.history[w.historyPosition].Message
				w.cursor = len(w.value)
				return true
			}
			return false
		},
		tcell.KeyDown: func(ev *tcell.EventKey) bool {
			if w.historyPosition > -1 {
				w.historyPosition--
				if w.historyPosition == -1 {
					w.value = ""
				} else {
					w.value = w.history[w.historyPosition].Message
				}
				w.cursor = len(w.value)
				return true
			}
			return false
		},
		tcell.KeyLeft: func(ev *tcell.EventKey) bool {
			if w.cursor > 0 {
				w.cursor--
				return true
			}
			return false
		},
		tcell.KeyRight: func(ev *tcell.EventKey) bool {
			if w.cursor < len(w.value) {
				w.cursor++
				return true
			}
			return false
		},
		tcell.KeyCtrlU: func(ev *tcell.EventKey) bool {
			w.value = w.value[w.cursor:]
			w.cursor = 0
			return true
		},
		tcell.KeyTab: func(ev *tcell.EventKey) bool {
			// Auto-complete
			// TODO: Find best guess for current word
			return false
		},
		tcell.KeyEnter: func(ev *tcell.EventKey) bool {
			// TODO: Submit the message
			return false
		},
	})
}
func (w *Chat) SetTitle(ttl string) { w.title = ttl }
func (w *Chat) Title() string       { return w.title }
func (w *Chat) SetValue(val string) {
	w.value = val
	w.cursor = len(val)
}

func (w *Chat) Log(msg *ChatMsg) {
	w.rawLog = append(w.rawLog, msg.String())
	w.log = append(w.log, msg)
}

func (w *Chat) Clear() {
	w.rawLog = []string{}
	w.log = []*ChatMsg{}
}

const (
	MsgChat = iota
	MsgSystem
)

type ChatMsg struct {
	MsgType  int
	MsgTime  time.Time
	FromName string
	Message  string
}

func (c ChatMsg) String() string {
	if c.MsgType == MsgChat {
		from := c.FromName
		if from == "" {
			from = "Unknown"
		}
		return fmt.Sprintf("%s <%s> %s", c.MsgTime.Format("15:04"), from, c.Message)
	}
	return fmt.Sprintf("%s", c.Message)
}
