# -*-coding:Latin-1 -*


from kloop import *
from item import *
from kanban import *
from place import *
from operation import *
from workshop import *
from nomenclature import *
#from ui_Fkanban import *

import time
import logging

class Fkanban():
	''' A Kanban environment
	'''
	def __init__(self, workshops = [], loops = [], speed = 1):
		''' Initialisation
			- workshops		:	list of workshop
			- loops			:	list of loop
		'''
		self.workshops = workshops
		
		self.loops = loops
		for loop in self.loops:
			loop.fkanban = self
			for ope in loop.item.operations:
				ope.fkanban = self
		for kb in self.kanbans:
			kb.fkanban = self
		
		self.time = 0
		self.speed = speed #speed in hour per seconde or per manual click
		self.automatic_time = False
		self._check_datas()
		self.actions = [self.run_kanbans, self.run_loops, self.chg_time]
		self.next_action = self.run_kanbans
		

	def _check_datas(self):
		'''Check if all dats is correct
		'''
		pass
		#TODO :
		#	- nomenclature : pas de doublon dans les liens ex : item("toto",[nomenclature(A,1),nomenclature(A,5)]
		
	@property
	def kanbans(self):
		''' Return the list of all kangans
		'''
		kbs = []
		for loop in self.loops:
			kbs+=loop.kanbans
		return kbs
	
	def run_kanbans(self):
		'''Update kanbans (producing)
		'''
		logging.debug("Update kanbans...")
		nb_actions = 0
		for kb in self.kanbans:
			nb_actions += kb.push()
		logging.debug("%s actions done"%(nb_actions))
		return nb_actions
	
	def run_loops(self):
		'''Update loops
		'''
		logging.debug("Update loops...")
		nb_actions = 0
		for loop in self.loops:
			logging.debug("Check loop %s"%(loop))
			nb_actions += loop.produce_if_needed()
		logging.debug("%s actions done"%(nb_actions))
		return nb_actions
	
	def chg_time(self):
		self.time +=1
		logging.debug("time change. It is %s"%(self.time))
		return False
		
	def run_actions(self):
		''' run one action
		'''
		if self.next_action()==0:
			self.next_action=self.next_next_action()
			
	def auto_run(self):
		''' run 
		'''
		while True: # todo : bouton stop
			self.run_actions()
			self.ui_update_kanbans()
			self.update()
			time.sleep(1./self.speed/2)
	
	def next_next_action(self):
		''' return the next next_action
		'''
		return self.actions[(self.actions.index(self.next_action)+1)%len(self.actions)]
		
	def first_match_loop(self, item, shop= None):
		''' Return the first loop with item
		'''
		i = 0
		while i<len(self.loops):
			if isinstance(self.loops[i],fab_kloop):
				#if self.loops[i].customer_shop==shop:
				if self.loops[i].item == item:
					return self.loops[i]
			i+=1
		