from ableton.v2.base import listens
from ableton.v2.control_surface.component import Component
from ableton.v2.control_surface.control import ToggleButtonControl

from ..util import ValueControl

SCALES = [ 'Major', 'Minor', 'Dorian', 'Mixolydian' , 'Lydian' , 'Phrygian' , 'Locrian', 'Whole Tone', 'Half-whole Dim.', 'Whole-half Dim.', 'Minor Blues', 'Minor Pentatonic', 'Major Pentatonic', 'Harmonic Minor', 'Harmonic Major', 'Dorian #4', 'Phrygian Dominant', 'Melodic Minor', 'Lydian Augmented', 'Lydian Dominant', 'Super Locrian', 'Bhairav', 'Hungarian Minor', '8-Tone Spanish', 'Hirajoshi', 'In-Sen', 'Iwato', 'Kumoi', 'Pelog Selisir', 'Pelog Tembung', 'Messiaen 3', 'Messiaen 4', 'Messiaen 5', 'Messiaen 6', 'Messiaen 7' ]

class SettingsComponent(Component):
	"A component that handles the Settings mode of the Exquis and the synchronization of the settings with the Live set"

	tempo = ValueControl(initial_value=120)
	root_note = ValueControl(initial_value=0)
	scale_index = ValueControl(initial_value=0)
	custom_scale = ValueControl(initial_value=[0, 2, 4, 5, 7, 9, 11])
	tempo_push = ToggleButtonControl()

	def __init__(self, *a, **k):
		super(SettingsComponent, self).__init__(*a, **k)
		self.on_tempo_changed.subject = self.song
		self.on_metronome_changed.subject = self.song
		if hasattr(self.song, "root_note"):
			self.on_root_note_changed.subject = self.song
		if hasattr(self.song, "scale_name"):
			self.on_scale_name_changed.subject = self.song
		if hasattr(self.song, "scale_intervals"):
			self.on_scale_intervals_changed.subject = self.song

	def update(self):
		super(SettingsComponent, self).update()
		self.on_tempo_changed()
		if hasattr(self.song, "root_note"):
			self.on_root_note_changed()
		if hasattr(self.song, "scale_name"):
			self.on_scale_name_changed()
		if hasattr(self.song, "scale_intervals"):
			self.on_scale_intervals_changed()

	@tempo.value
	def on_tempo(self, v, _):
		self.song.tempo = v

	@listens("tempo")
	def on_tempo_changed(self):
		self.tempo.value = int(self.song.tempo)
	
	@tempo_push.released_immediately
	def on_tempo_push_pressed(self, _):
		self.song.tap_tempo()
	
	@tempo_push.pressed_delayed
	def on_tempo_push_pressed_delayed(self, _):
		self.song.metronome = not self.song.metronome
	
	@listens("metronome")
	def on_metronome_changed(self):
		self.tempo_push.is_toggled = self.song.metronome
	
	@root_note.value
	def on_root_note(self, v, _):
		if hasattr(self.song, "root_note"):
			self.song.root_note = v

	@listens("root_note")
	def on_root_note_changed(self):
		self.root_note.value = self.song.root_note

	@scale_index.value
	def on_scale_index(self, v, _):
		if v >= len(SCALES):
			v = len(SCALES) - 1
		self.song.scale_name = SCALES[v]

	@listens("scale_name")
	def on_scale_name_changed(self):
		try:
			scale_index = SCALES.index(self.song.scale_name)
		except:
			return
		self.scale_index.value = scale_index

	@listens("scale_intervals")
	def on_scale_intervals_changed(self):
		self.custom_scale.value = self.song.scale_intervals

