

Controller   = window.require 'spine/controller'
Container    = window.require 'spine/controller/container'
Activateable = window.require 'spine/controller/activateable'

Selector 	= require './selector'
Editor   	= require './editor'
SavePopup 	= require './save'
DeletePopup = require './delete'
SkinModel   = window.require 'core/models/skin'
Composition = window.require 'core/models/composition'

class SkinEditor extends Controller
	className	: 'skin-config'
	title		: 'Theme'
	nextTitle	: 'Skin'

	@include Container
	@include Activateable

	controllers:
		'selector'	: Selector
		'editor'	: Editor

	events:
		'saveItem'	: 'saveItem'
		'deleteItem': 'deleteItem'
		'SkinItem:selected': '_onSkinItemSelected'

	constructor: ->
		super

		@savePopup = new SavePopup({place: @el})
		@deletePopup = new DeletePopup({place: @el})

	setPremium: (@premium) ->
		@editor.setAccessLevel +@premium

	initSave: -> @selector.initSave()

	hasPendingChange: -> @selector.hasPendingChange()

	saveItem:(e,opts={}) ->
		e.stopPropagation()
		@savePopup.saveItem opts.controller, opts.item

	deleteItem:(e,opts={}) ->
		e.stopPropagation()
		@deletePopup.deleteItem opts.controller, opts.item

	select: (skin, force = false) ->
		return false unless skin.widget_id is @widget.id
		selectedSkin = skin
		@selector.select(selectedSkin.id, force)

	for: (@composition) ->
		@widget = @composition.getWidget()
		@initialize()
		@

	initialize: (options) =>
		return console.warn 'No composition set' unless @composition
		return console.warn 'No widget set' unless @widget

		@stopListening()
		@listenTo SkinModel, 'save',   @_onSkinSave
		@listenTo SkinModel, 'destroy',@_onSkinDestroy
		@listenTo SkinModel, 'create', @_onSkinCreate
		@listenTo SkinModel, 'change', @_onSkinChange

		skins = SkinModel.findAllByAttribute('widget_id', @widget.id)
		skins = skins.filter((i) -> i.isPreset()).concat skins.filter (i) -> !i.isPreset()
		meta = @widget.skinMeta

		@editor.parseMeta meta, @composition
		@selector.refresh skins

		@editor.display()

		@select(@composition.getSkin() or skins[0])

		@trigger 'initialized'

	_onSkinSave: (skin, opts={}) =>
		@$trigger 'skin:save', skin if opts.ajax or opts.fromApi

	_onSkinDestroy: (skin, opts) =>
		@$trigger 'skin:destroy', skin

	_onSkinCreate: (skin) =>
		return if skin.widget_id isnt @widget?.id
		@initialize()
		@selector.toggleBtn()
		@selector.select(skin.id)

	_onSkinItemSelected: (ev, skin) =>
		@composition.updateAttribute('skin_id', skin.id, {ajax: false})
		@currentSkin?.used(-1)
		@currentSkin = skin
		@currentSkin.used(+1)
		@editor.setSkin(skin)

	_onSkinChange: (skin, type, options) =>
		@editor.skinChanged(skin)
		return unless @currentSkin?.eql(skin)
		return unless options.changedID
		@composition.updateAttribute('skin_id', skin.id, {ajax: false})

	activate: (step) =>
		Activateable.activate.apply @, arguments
		@$trigger 'viewChange', 'skin'
		@[step]?()
		@

	edit_skin: =>
		@delay @selector.editSkinState, 15

	setOptions: (opts) =>
		@selector.hideEditBtn() if opts.enabled? and !opts.enabled

	release: =>
		@currentSkin.reset()
		super

module.exports = SkinEditor
