import sublime
from Default.exec import ExecCommand
import os
from . import CommonFunctions
from .BSSCompiler import BSSCompiler
from .Spinner import Spinner

class MyExecCommand(ExecCommand):

	def run(self, 
		on_finished_func_desc,
		need_spinner = True,
		cmd = None, 
		shell_cmd = None, 
		file_regex = '', 
		line_regex = '', 
		working_dir = '', 
		encoding = 'utf-8', 
		env = {}, 
		quiet = False, 
		kill = False, 
		update_phantoms_only = False, 
		hide_phantoms_only = False, 
		word_wrap = True, 
		syntax = 'Packages/Text/Plain text.tmLanguage',
		**kwargs):		
		self.on_finished_func_desc = on_finished_func_desc
		self.need_spinner = need_spinner
		self.processResultStr = ''
		if self.need_spinner:
			self.spinner = Spinner(Spinner.SYMBOLS_BOX, sublime.active_window().active_view(), 'BSScript: ', '')
			self.spinner.start()
		ExecCommand.run(self, cmd, shell_cmd, file_regex, line_regex, working_dir, encoding, env, quiet, kill, update_phantoms_only, hide_phantoms_only, word_wrap, syntax, **kwargs)

	def on_finished(self, proc):
		ExecCommand.on_finished(self, proc)
		if BSSCompiler.RESULT_STR_SUCCESS not in self.processResultStr and \
			BSSCompiler.RESULT_STR_WARNINGS not in self.processResultStr:
			blsSubStr = (self.debug_text[:self.debug_text.find('.bls') + 4])
			blsPathStr = blsSubStr[blsSubStr.rfind("'") + 1:]
			print('BSScript: ' + blsPathStr + ' not compiled!')
			sublime.active_window().active_view().set_status(CommonFunctions.SUBLIME_STATUS_LOG, 'BSSCompiler: ' + blsPathStr + ' not compiled!')			
			return
		if self.on_finished_func_desc:
			for functionName, functionParams in self.on_finished_func_desc.items():				
				fn = None
				if hasattr(CommonFunctions, functionName):
					fn = getattr(CommonFunctions, functionName)
				elif hasattr(BSSCompiler, functionName):
					fn = getattr(BSSCompiler, functionName)
				
				if fn:
					if functionParams:
						fn(functionParams)
					else:
						fn()
			if self.spinner:
				self.spinner.stop()

	def on_data(self, proc, data):
		ExecCommand.on_data(self, proc, data)
		self.processResultStr = self.processResultStr + data.decode(self.encoding)
@@ -1535,22 +1535,18 @@
 d!')
-%09%09%09%0A%09%09%09return%0A
+%0A%09%09else:%0A%09
 %09%09if
@@ -1577,16 +1577,17 @@
 esc:%0A%09%09%09
+%09
 for func
@@ -1654,24 +1654,25 @@
 ():%09%09%09%09%0A%09%09%09%09
+%09
 fn = None%0A%09%09
@@ -1669,16 +1669,17 @@
  = None%0A
+%09
 %09%09%09%09if h
@@ -1709,32 +1709,33 @@
  functionName):%0A
+%09
 %09%09%09%09%09fn = getatt
@@ -1771,16 +1771,17 @@
 me)%0A%09%09%09%09
+%09
 elif has
@@ -1814,24 +1814,25 @@
 Name):%0A%09%09%09%09%09
+%09
 fn = getattr
@@ -1867,20 +1867,23 @@
 %09%09%09%09
+%09
 %0A%09%09%09%09
+%09
 if fn:%0A
+%09
 %09%09%09%09
@@ -1908,16 +1908,17 @@
 :%0A%09%09%09%09%09%09
+%09
 fn(funct
@@ -1928,16 +1928,17 @@
 Params)%0A
+%09
 %09%09%09%09%09els
@@ -1950,14 +1950,14 @@
 %09%09%09%09
+%09
 fn()%0A
-%09
 %09%09if
@@ -1971,17 +1971,16 @@
 pinner:%0A
-%09
 %09%09%09self.

fix spinner