import pygame
import main					#able to load the lib
main.init()					#
import data, color				#then load the libs
from color import *

#once activated , turn any obj into spring


def init():
	global springnator_img
	global springnator1_img
	global springnator2_img
	global springnator3_img
	global springnator_rect
	springnator_img = data.load_img('SPRINGMINATOR.png')
	springnator1_img = data.load_img('SPRINGMINATOR01.png')
	springnator2_img = data.load_img('SPRINGMINATOR02.png')
	springnator3_img = data.load_img('SPRINGMINATOR03.png')
	springnator_rect = springnator_img.get_rect()
	springnator_img.convert()
	springnator1_img.convert()
	springnator2_img.convert()
	springnator3_img.convert()
	springnator_img.set_colorkey(white)
	springnator1_img.set_colorkey(white)
	springnator2_img.set_colorkey(white)
	springnator3_img.set_colorkey(white)

class springminator(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.image, self.rect = springnator_img, springnator_rect
		self.rect = self.rect.move(120,380)
		self.spr = None
		self.wait = 0

	def update(self):
		if self.spr != None:
			if self.spr == False:
				self.spr = None
			elif self.spr == True:
				if self.wait == 0:
					self.wait = 10
					if self.image == springnator3_img:
						self.image = springnator_img
						self.spr = False
					elif self.image == springnator2_img:
						self.image = springnator3_img
					elif self.image == springnator1_img:
						self.image = springnator2_img
				else:
					self.wait -= 1
			else:
				self.spr.kill()
				self.image = springnator1_img
				self.spr = True

	def draw(self,screen):
		screen.blit(self.image, self.rect)

	def convert(self,spr):
		self.spr = spr
		

if __name__=="__main__":
	#tester
	pygame.init()					#first
	screen = pygame.display.set_mode((640,480))	#the screen
	clock = pygame.time.Clock()			#the time

	#accelerator
	exit_button = pygame.QUIT
	pygame.event.set_blocked(None)
	pygame.event.set_allowed(exit_button)
	happen = pygame.event.peek
	fps = clock.tick
	flip = pygame.display.flip
	cls = screen.fill
	white = color.white

	#springminator
	init()
	machine = springminator()

	#kossin
	import kossin
	import random
	kossin.init()
	kossins = pygame.sprite.Group()
	for i in range(20):
		kossins.add(kossin.kossin(random.randint(80,540),random.randint(80,400)))

	#spring
	import spring
	spring.init()
	springs = pygame.sprite.Group()

	#cube
	import grapping_cube
	grapping_cube.init()
	cube = grapping_cube.grappling_cube()
	taken = None

	#main loop
	end = False
	raize = False
	button = False
	while not end:
		fps(40)
		flip()
		cls(white)
		if happen(exit_button):
			end = True
		else:
			pygame.event.clear()
		if True in pygame.mouse.get_pressed():
			if not button:
				raize = True
				button = True
			else:
				raize = False
		else:
			raize = False
			button = False
		#test springnator
		machine.update()
		machine.draw(screen)
		kossins.update()
		kossins.draw(screen)
		#test cube
		if raize :
			(x,y) = pygame.mouse.get_pos()
			if not taken and not cube.spr:
				for kossin in kossins:
					if kossin.rect.collidepoint(x,y):
						cube.take(kossin)
						taken = kossin
			elif taken and cube.destx == 0 and cube.desty == 0:
				if machine.rect.collidepoint(x,y) and not machine.spr:
					cube.go((machine.rect.centerx,machine.rect.top))
					taken = None
		cube.update()
		cube.draw(screen)
		for sp in pygame.sprite.spritecollide(machine,kossins,0):
			if not cube.spr:
				machine.convert(sp)
		if machine.spr == False:
			springs.add(spring.spring(machine.rect.centerx,machine.rect.top,grey))
		springs.update()
		springs.draw(screen)
