#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is part of ROBOTGAME
#
# ROBOTGAME is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# ROBOTGAME is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# ROBOTGAME.  If not, see <http://www.gnu.org/licenses/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
#                         level3.py
#                     --------------------
#       date created : Wed Aug 8 2012
#          copyright : (C) 2012 Niels G. W. Serup
#      maintained by : Niels G. W. Serup <ns@metanohi.name>

"""
The third level.
"""

import os
import pygame
import random
import re
import itertools

import worldobject
import level
import player
import tile
import block
import boulder
import lever

import logic.colourboxes


class Level3(level.Level):
    def __init__(self, game, graphics_dir, paused=False):
        level.Level.__init__(self, game, graphics_dir, size=(64*20, 48*20),
                             paused=paused)

        self.dimensions = 10, 10

        # for i in range(self.dimensions[0]):
        #     for j in range(self.dimensions[1]):
        #         self.tiles.append(
        #             tile.Tile(self, i*64, j*48, self.imgs['ground1']))

        task_start = (2, 2)

        # Abstract "boxes", actually colour fields
        boxes = []
        for i in range(4):
            boxes.extend([(0, 0, 0)] * i + box + [(0, 0, 0)] * (3 - i)
                         for box in logic.colourboxes.generate_colour_boxes(1, 3))
        boxes.extend(logic.colourboxes.generate_random_box(4, 2) for _ in range(20))
        boxes.extend([(0, 0, 0)] * 4 for _ in range(10))
        random.shuffle(boxes)
        pos_colour = {}
        for box, (x, y) in zip(boxes, itertools.product(range(7), range(6))):
            self.tiles.append(tile.Tile(self, 64 * (x + task_start[0] + 1),
                                        48 * (y + task_start[1] + 1),
                                        self.imgs['indoor%d' % random.randint(1, 6)]))
            pos_colour[(x, y)] = box

        self.draw_background()

        action_blocks = list(itertools.chain(*
                [(block.ActionBlock(self, 64 * task_start[0],
                                    48 * (i + 1 + task_start[1]),
                                    movable=True),
                  block.ActionBlock(self, 64 * (task_start[0] + 8),
                                    48 * (i + 1 + task_start[1]),
                                    movable=True))
                 for i in range(6)]))
        self.objects.extend(action_blocks)

        wells = [block.ColorWell(self, task_start[0] * 64, task_start[1] * 48),
                 block.ColorWell(self, (task_start[0] + 8) * 64, task_start[1] * 48),
                 block.ColorWell(self, task_start[0] * 64, (task_start[1] + 7) * 48),
                 block.ColorWell(self, (task_start[0] + 8) * 64, (task_start[1] + 7) * 48),
                 ]
        self.objects.extend(wells)

        def update_wells(block):
            cur_boxes = []
            for block in action_blocks:
                box = pos_colour.get((block.x / 64 - task_start[0] - 1,
                                      block.y / 48 - task_start[1] - 1))
                if box:
                    cur_boxes.append(box)

            if not cur_boxes:
                well_colours = [(0, 0, 0)] * len(wells)
            else:
                well_colours = logic.colourboxes.get_colours(cur_boxes)
            for well, color in zip(wells, well_colours):
                well.set_colour(*color)

        for b in action_blocks:
            b.action = update_wells


    def restart(self):
        for obj in self.objects:
            obj.reset_pos()
