# -*- coding: utf-8 -*-
from __future__ import division
from .__load__ import *

class Robot(object):
	def init(self, profiling=False, logout=False):
		# 大脑
		self.brain = Demeter.service('brain').init(self)
		# 耳朵
		self.ear = Demeter.service('ear').init(self)
		# 嘴
		self.mouth = Demeter.service('mouth').init(self)
		# 眼睛
		self.eye = Demeter.service('eye').init(self)

		# 行为工具
		self.tool = {}
		# 数据文件夹
		self.data = Demeter.config['vecan']['data']
		# 日志
		Demeter.logger = self.logger = Log.init(__name__, logout)

		# 是否正在录音
		self.isRecording = False
		# 插件验证--暂时无用
		self.matchPlugin = None
		# 勿扰模式
		self.immersiveMode = None
		# 优化
		self.profiling = profiling
		
		# 醒来 启动
		self.wakeup()

	def wakeup(self):
		# 机器人配置，后续改成从数据库读取
		self.config = Demeter.config['robot']
		for item in self.config:
			setting = self.setting(self.config[item])
			if item not in self.tool:
				self.logger.info('使用' + self.config[item] + ' ' + item + '引擎')
				self.tool[item] = Demeter.service(self.config[item], 'api')
				self.tool[item].setting(**setting)
		self.brain.wakeup()
		

	def wait(self):
		try:
			self.ear.wait()
		except AttributeError:
			self.logger.critical('初始化离线唤醒功能失败')

	def sleep(self, signal, frame):
		clean()
		self.brain.pause()
		self.ear.close()
		self.mouth.close()
		self.eye.close()
		Demeter.dog.stop()

	def say(self, msg, cache=True, plugin='', completed=None):
		self.mouth.say(msg, cache, plugin, completed)

	def checkRestore(self):
		if self.immersiveMode:
			self.brain.restore()

	def setting(self, name):
		# 后续要改成数据库读取
		config = Demeter.config[name]
		return config

	# 中断、停止
	def stop(self):
		self.mouth.close()
		if self.immersiveMode:
			self.brain.pause()

	def doParse(self, query, **args):
		return self.tool['nlu'].nlu(query, **args)

	def setImmersiveMode(self, slug):
		self.immersiveMode = slug

	def getImmersiveMode(self):
		return self.immersiveMode