#!/usr/bin/env python
import os
import sys

import pygame
from pygame.locals import *
from interactive.run_raichu import RaichuManager
from interactive.buttons import draw_buttons
import interactive.images
from interactive.style import SIZE, WHITE, BLACK, FATTY_ACID_IMAGE_SIZE
from interactive.background import ButtonPanel, ProductRaster
from interactive.load_antismash import genes_from_antismash
from interactive.load_tabular import load_tabular

IMAGE_DIR = os.path.dirname(interactive.images.__file__)

size = SIZE
width, height = size

pygame.init()
screen = pygame.display.set_mode(size)
screen.fill(WHITE)

button_panel = ButtonPanel(screen)
button_panel.draw()

raichu_icon = pygame.image.load(os.path.join(IMAGE_DIR, 'icon.png'))

pygame.display.set_caption('RAIChU')
pygame.display.set_icon(raichu_icon)

running = True

manager = RaichuManager(screen)
product_raster = ProductRaster(screen)

input_file = None

if len(sys.argv) == 2:
    input_file = sys.argv[1]
    if input_file.endswith('.gbk'):
        manager.genes = genes_from_antismash(input_file, screen)
    elif input_file.endswith('.txt'):
        manager.genes = load_tabular(input_file, screen)


while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

        if event.type == MOUSEBUTTONDOWN:
            mouse = pygame.mouse.get_pos()
            manager.do_click_action(mouse)

        if event.type == pygame.KEYDOWN:
            if manager.text_box:
                if event.key == pygame.K_RETURN:
                    pass
                elif event.key == pygame.K_BACKSPACE:
                    manager.text_box.remove_character()
                else:
                    manager.text_box.add_character(event.unicode)

    if manager.text_box:
        manager.text_box.draw(screen)

    if not manager.cluster_image and not manager.product_images:
        button_panel.draw()
        mouse = pygame.mouse.get_pos()
        if manager.fatty_acid:
            rectangle = pygame.Rect(SIZE[0] - (FATTY_ACID_IMAGE_SIZE[0] + 20), SIZE[1] / 2 + 20,
                                    manager.fatty_acid.scaled_image.get_width(), manager.fatty_acid.scaled_image.get_height())
            pygame.draw.rect(screen, WHITE, pygame.Rect(SIZE[0] - (FATTY_ACID_IMAGE_SIZE[0] + 20), SIZE[1] / 2 + 20,
                             FATTY_ACID_IMAGE_SIZE[0], FATTY_ACID_IMAGE_SIZE[1]))
            screen.blit(manager.fatty_acid.scaled_image, rectangle)
            pygame.draw.rect(screen, BLACK, rectangle, 2)

        draw_buttons(manager.active_buttons, screen, mouse)
        for gene in manager.genes:
            gene.draw(mouse)
    elif manager.cluster_image:
        width = manager.cluster_image.get_width()
        height = manager.cluster_image.get_height()
        x = SIZE[0] - width
        y = 0
        rectangle = pygame.Rect(x, y, width, height)

        screen.blit(manager.cluster_image, rectangle)
        pygame.draw.rect(screen, BLACK, rectangle, 2)
    elif manager.product_images:
        product_raster.draw_images(manager.product_images)

    pygame.display.update()

pygame.quit()
