# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-10-02 18:35
from __future__ import unicode_literals

from django.db import migrations
import numpy as np

RATIO = 10000

class Box(object):
    def __init__(self, parent, id_num, y1, y2, x1, x2):
        self.parent = parent
        self.id_num = id_num
        self.x1     = x1
        self.x2     = x2
        self.y1     = y1
        self.y2     = y2
        self.area   = (x2 - x1) * (y2 - y1)

def do_stuff(apps, schemd_editor):
    DetectedCombinedBox = apps.get_model('calligraphy', 'DetectedCombinedBox').objects
    Pages = apps.get_model('calligraphy', 'Page').objects.filter(has_copyright_restrictions=True)
    for curPage in Pages:
        PAGE_THRESHOLD = (curPage.image_length * curPage.image_width) / RATIO
        box_dict = dict()
        grid = np.zeros((curPage.image_length, curPage.image_width), dtype=np.int)
        boxes = DetectedCombinedBox.filter(parent_page=curPage)
        box_to_review = []
        for box in boxes:
            grid[box.y1:box.y2, box.x1:box.x2].fill(box.id)
            box_dict[box.id] = box
            if (box.x2 - box.x1) * (box.y2 - box.y1) < PAGE_THRESHOLD:
                box_to_review.append(Box(box, box.id, box.y1, box.y2, box.x1, box.x2))
        box_to_review.sort(key=lambda box_to_review: box_to_review.area)
        box_removed_from_review = []
        box_delete_que = []
        for box in box_to_review:
            print(box.id_num)
            if box.id_num not in box_removed_from_review:
                box_delete_que.append(box.parent)
                max_search_distance = max(box.y2 - box.y1, box.x2 - box.x1)
                keep_going = True
                for i in range(max_search_distance):
                    if keep_going:
                        y1_new = max(box.y1 - i, 0)
                        y2_new = min(box.y2 + i, curPage.image_length)
                        x1_new = max(box.x1 - i, 0)
                        x2_new = min(box.x2 + i, curPage.image_width)
                        uniques = np.unique(grid[y1_new:y2_new, x1_new:x2_new])
                        if len(uniques) > 2:
                            for unique in uniques:
                                if unique != 0 and unique != box.id_num:
                                    box_grow = box_dict[unique]
                                    box_removed_from_review.append(unique)
                                    box_grow.y1 = min(box_grow.y1, box.y1)
                                    box_grow.y2 = max(box_grow.y2, box.y2)
                                    box_grow.x1 = min(box_grow.x1, box.x1)
                                    box_grow.x2 = max(box_grow.x2, box.x2)
                                    grid[box_grow.y1:box_grow.y2, box_grow.x1:box_grow.x2].fill(box_grow.id)
                                    box_grow.save()
                                    keep_going = False
        for box in box_delete_que:
            box.delete()

class Migration(migrations.Migration):

    dependencies = [
        ('calligraphy', '0188_auto_20171002_1833'),
    ]

    operations = [ migrations.RunPython(do_stuff)
    ]
