package cview

import (
	

	
)

// Button is labeled box that triggers an action when selected.
type Button struct {
	*Box

	// The text to be displayed before the input area.
	label []byte

	// The label color.
	labelColor tcell.Color

	// The label color when the button is in focus.
	labelColorFocused tcell.Color

	// The background color when the button is in focus.
	backgroundColorFocused tcell.Color

	// An optional function which is called when the button was selected.
	selected func()

	// An optional function which is called when the user leaves the button. A
	// key is provided indicating which key was pressed to leave (tab or backtab).
	blur func(tcell.Key)

	// An optional rune which is drawn after the label when the button is focused.
	cursorRune rune

	sync.RWMutex
}

// NewButton returns a new input field.
func ( string) *Button {
	 := NewBox()
	.SetRect(0, 0, TaggedStringWidth()+4, 1)
	.SetBackgroundColor(Styles.MoreContrastBackgroundColor)
	return &Button{
		Box:                    ,
		label:                  []byte(),
		labelColor:             Styles.PrimaryTextColor,
		labelColorFocused:      Styles.PrimaryTextColor,
		cursorRune:             Styles.ButtonCursorRune,
		backgroundColorFocused: Styles.ContrastBackgroundColor,
	}
}

// SetLabel sets the button text.
func ( *Button) ( string) {
	.Lock()
	defer .Unlock()

	.label = []byte()
}

// GetLabel returns the button text.
func ( *Button) () string {
	.RLock()
	defer .RUnlock()

	return string(.label)
}

// SetLabelColor sets the color of the button text.
func ( *Button) ( tcell.Color) {
	.Lock()
	defer .Unlock()

	.labelColor = 
}

// SetLabelColorFocused sets the color of the button text when the button is
// in focus.
func ( *Button) ( tcell.Color) {
	.Lock()
	defer .Unlock()

	.labelColorFocused = 
}

// SetCursorRune sets the rune to show within the button when it is focused.
func ( *Button) ( rune) {
	.Lock()
	defer .Unlock()

	.cursorRune = 
}

// SetBackgroundColorFocused sets the background color of the button text when
// the button is in focus.
func ( *Button) ( tcell.Color) {
	.Lock()
	defer .Unlock()

	.backgroundColorFocused = 
}

// SetSelectedFunc sets a handler which is called when the button was selected.
func ( *Button) ( func()) {
	.Lock()
	defer .Unlock()

	.selected = 
}

// SetBlurFunc sets a handler which is called when the user leaves the button.
// The callback function is provided with the key that was pressed, which is one
// of the following:
//
//   - KeyEscape: Leaving the button with no specific direction.
//   - KeyTab: Move to the next field.
//   - KeyBacktab: Move to the previous field.
func ( *Button) ( func( tcell.Key)) {
	.Lock()
	defer .Unlock()

	.blur = 
}

// Draw draws this primitive onto the screen.
func ( *Button) ( tcell.Screen) {
	if !.GetVisible() {
		return
	}

	.Lock()
	defer .Unlock()

	// Draw the box.
	 := .borderColor
	 := .backgroundColor
	if .focus.HasFocus() {
		.backgroundColor = .backgroundColorFocused
		.borderColor = .labelColorFocused
		defer func() {
			.borderColor = 
		}()
	}
	.Unlock()
	.Box.Draw()
	.Lock()
	.backgroundColor = 

	// Draw label.
	, , ,  := .GetInnerRect()
	if  > 0 &&  > 0 {
		 =  + /2
		 := .labelColor
		if .focus.HasFocus() {
			 = .labelColorFocused
		}
		,  := Print(, .label, , , , AlignCenter, )

		// Draw cursor.
		if .focus.HasFocus() && .cursorRune != 0 {
			 :=  + int(float64()/2+float64()/2)
			if  > +-1 {
				 =  +  - 1
			} else if  < + {
				++
			}
			Print(, []byte(string(.cursorRune)), , , , AlignLeft, )
		}
	}
}

// InputHandler returns the handler for this primitive.
func ( *Button) () func( *tcell.EventKey,  func( Primitive)) {
	return .WrapInputHandler(func( *tcell.EventKey,  func( Primitive)) {
		// Process key event.
		if HitShortcut(, Keys.Select, Keys.Select2) {
			if .selected != nil {
				.selected()
			}
		} else if HitShortcut(, Keys.Cancel, Keys.MovePreviousField, Keys.MoveNextField) {
			if .blur != nil {
				.blur(.Key())
			}
		}
	})
}

// MouseHandler returns the mouse handler for this primitive.
func ( *Button) () func( MouseAction,  *tcell.EventMouse,  func( Primitive)) ( bool,  Primitive) {
	return .WrapMouseHandler(func( MouseAction,  *tcell.EventMouse,  func( Primitive)) ( bool,  Primitive) {
		if !.InRect(.Position()) {
			return false, nil
		}

		// Process mouse event.
		if  == MouseLeftClick {
			()
			if .selected != nil {
				.selected()
			}
			 = true
		}

		return
	})
}