
import xml.dom.minidom
import util
import pygame
import shipsettings
import cockpit
import parts

###########################################################################
# Sundog: Resurrection
# code by Patrick Devine (patrick@cpan.org)
#
#
#

from pygame.locals import *

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

class ShipInterior:
    def __init__(self, screen, file="ship.xml"):
        self.screen = screen

        self.parseConfigFile(file)

    def parseConfigFile(self, file):
        doc = xml.dom.minidom.parse(file)

        self.shipSystems = {}

        for node in doc.getElementsByTagName("interior"):
            image = util.fetchTextFromElementByTagName(node, "image")

            for bay in node.getElementsByTagName("bay"):
                type = bay.getAttribute("type")
                image_name = \
                    util.fetchTextFromElementByTagName(bay, "image_name")

                if type == "warp":
                    self.shipSystems["warp"] = \
                        WarpBay(self.screen, image_name)

        (self.interior_img, self.interior_rect) = util.load_image(image)

    def releaseActiveWindow(self):
        for system in self.shipSystems.keys():
            if self.shipSystems[system].active:
                self.shipSystems[system].active = False
                return

    def draw(self):
        self.screen.blit(self.interior_img, (0, 0))

        for system in self.shipSystems.keys():
            self.shipSystems[system].draw()

class Bay:
    x_offset = 250
    y_offset = 120

    def __init__(self, screen, image):
        self.screen = screen

        (self.interior_bay_img, self.interior_bay_rect) = \
            util.load_image(image)

        self.nodes = pygame.sprite.Group()

        self.active = False

    def draw(self):
        if self.active:
            self.screen.blit(self.interior_bay_img,
                             (self.x_offset, self.y_offset))

            for part in self.nodes:
                self.screen.blit(part.image, part.rect)

    def loadSprites(self, bay):
        """Loads the various sprites for each of the parts in the
           ships system bays.

           This could probably use some serious cleanup.
        """

        partMap = {
            shipsettings.PART_CONTROLNODE: parts.ControlNode,
            shipsettings.PART_FLUXMOD: parts.FluxModulator,
            shipsettings.PART_PHOTON: parts.PhotonBridge,
            shipsettings.PART_STDISTORT: parts.STDistorter,
            shipsettings.PART_CRYOFUSE: parts.CryoFuse,
            shipsettings.PART_PLASMATUBE: parts.PlasmaTube,
            shipsettings.PART_SCANNER: parts.Scanner,
            shipsettings.PART_JJUNCMODULE: parts.JJuncModule,
            shipsettings.PART_SHUNT: parts.Shunt,
            shipsettings.PART_BROKEN: parts.Broken
        }

        x = 413
        y = 147
        for row in bay:
            for slot in row:

                if partMap.has_key(slot):
                    self.nodes.add(partMap[slot]((x, y)))

                x += 64
            x = 413
            y += 64


class WarpBay(Bay):
    def __init__(self, screen, image):
        Bay.__init__(self, screen, image)

        ship = shipsettings.Ship()
        self.loadSprites(ship.warp)
        
class SubCEngines(Bay):
    def __init__(self, screen, image):
        Bay.__init__(self, screen, image)

        ship = shipsettings.Ship()
        self.loadSprites(ship.subc)

if __name__ == "__main__":

    pygame.init()
    window = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption('SunDog: Python Ressurrection')

    screen = pygame.display.get_surface()

    cockpit = cockpit.Cockpit(screen)
    interior = ShipInterior(screen)

    drawCockpit = False

    interior.draw()

    pygame.display.flip()

    clock = pygame.time.Clock()

    while True:
        clock.tick(60)

        for event in pygame.event.get():
            if event.type == KEYUP:
                if event.key == K_1:
                    drawCockpit = True
                elif event.key == K_4:
                    interior.shipSystems['warp'].active = True
            elif event.type == MOUSEBUTTONUP:
                if event.button == 3 and not cockpit.starfield.freeview:
                    interior.releaseActiveWindow()
                    drawCockpit = False
            if drawCockpit:
                cockpit.handleEvents(event)

        if drawCockpit:
            cockpit.draw()
        else:
            interior.draw()

        pygame.display.flip()

