

Controller = window.require 'spine/controller'

Skin    = window.require 'core/models/skin'
Popup	= require '../popup'

skin =
	confirmBtnClass: 'green-button button--uc button--save create-button'
	cancelBtnClass: 'dim-button button--uc button--overwrite override-button'
	title: 'How do you want to proceed?'
	content: '<input class="general-input skin-name" type="text" placeholder="Untitled Theme" />'
	note: 'Changes will also apply to previous uses of this theme.'
	confirmBtn: 'Create a new theme'
	cancelBtn: 'Overwrite previous theme'
	separator: true

preset =
	confirmBtnClass: skin.confirmBtnClass
	cancelBtnClass: skin.cancelBtnClass
	title: 'Create a new theme?'
	content: skin.content
	confirmBtn: 'Create'
	separator: false

class SavePopup extends Controller
	constructor:->
		super
		@popup = new Popup className: 'popup skin-save-popup'
		@listenTo @popup, 'canceled', @_onSaveCanceled

	saveItem: (item)->
		@model = (@item = item).item
		@popup.render(if @model.user_id? then skin else preset).pop @place, @save, @override

	save: =>

		atts = @model.attributes()
		delete atts['id']
		delete atts['uses']
		atts = $.extend {}, atts , { name:@popup.el.find('input').val(),user_id:1}

		@item.item.reset()
		s = new Skin(atts)
		s.save()
		@item.changes={}

	override: =>

		@item.override()
		@item.item.save()

	_onSaveCanceled: =>
		@model.trigger('save-canceled')

	_closePopup: => @popup.close()

module.exports = SavePopup
