
# TODO: on modification, the description isn't updated on the client

h = require 'maquette' .h
Modal = require './modal.ls'

{field, input, textarea, label, control, select} = require './bulma.ls'

# FIXME: This be a copy-pasta. Somebody gotta touch this spaghetti.
const PERMISSION_LEVELS = ["admin", "edit", "read"]

colors = [
	"white"
	"black"
	"light"
	"dark"
	"primary"
	"info"
	"link"
	"success"
	"warning"
	"danger"
	"black-bis"
	"black-ter"
	"grey-darker"
	"grey-dark"
	"grey"
	"grey-light"
	"grey-lighter"
	"white-ter"
	"white-bis"
]

column-form-selection = (self, column) ->
	h \option {
		value: column.id
		selected: self.extra_properties && self.extra_properties.column == column.id
	} [ column.title ]

color-to-form-selection = (self, color, current-color) ->
	h \option {
		value: color
		selected: current-color && current-color == color
	} [ color ]

user-form-selection = (users-cache, uid, is-selected) ->
	h \option {
		selected: is-selected
		value: uid.to-string!
	} [
		if user = users-cache.get-user uid
			(user.profile?.full_name) || user.login
		else
			uid.to-string!
	]

TaskCreationModal = (project, todod-ws, task, users-cache) ->
	task ||= {}

	# copy not to override anything on cancel
	self = {
		title: task.title || ""
		description: task.description || ""
		assigned_to: task.assigned_to
		extra_properties:
			column: ""
			background-color: ""
	}

	# copy extra properties
	# currently: column + background-color + assignee + expected duration time
	for k,v of task.extra_properties
		self.extra_properties[k] = v

	if self.extra_properties.column == ""
		self.extra_properties.column = project.extra_properties.columns?[0]?.id || ""

	modal = Modal {
		+visible
		content-render: (self) ->
			h \div.form [

				#
				# TITLE
				#

				field [
					label "Task title"

					input {
						value: self.title
						oninput: (e) ->
							self.title := e.target.value
					}
				]


				field [
					label "Task description"

					#
					# DESCRIPTION
					#

					textarea {
						value: self.description
						oninput: (e) ->
							self.description := e.target.value
					}
				]


				field [
					label "Column"

					control [
						select \.is-fullwidth {
							onchange: (e) ->
								self.extra_properties.column := e.target.value
						} project.extra_properties.columns.map (column) -> column-form-selection self, column
					]
				]


				#
				# USER MANAGEMENT
				#

				h \hr []

				field [
					label "Assigned user"

					control {
						key: "assign-someone-to-task"
					} [
						select \.is-fullwidth {
							onchange: (e) ->
								if uid = parse-int e.target.value
									self.assigned_to := uid
								else
									self.assigned_to := void
						} [
							h \option {
								default: true
								value: ""
							} [ "(unassigned)" ]

							for permission in PERMISSION_LEVELS
								[
									for uid in project.permissions[permission]
										user-form-selection users-cache, uid,
											(self.assigned_to == uid)
								]
						]
					]
				]


				#
				# BACKGROUND COLOR
				#

				field [
					label "Card background color"

					control [
						select \.is-fullwidth {
							onchange: (e) ->
								self.extra_properties.background-color := e.target.value
						} colors.map (color) -> color-to-form-selection self, color, self.extra_properties.background-color
					]
				]
			]

		on-validation: ->
			tmp = delete self.tmp
			if task.id
				todod-ws.edit-task task.id, self
			else
				todod-ws.add-task project.id, self.title, self
			self.tmp = tmp
	}, self

	self.render = ->
		modal.render!

	self

module.exports = TaskCreationModal
