# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-09-18 13:31
#
# Delete features that are a different color than the character
#
#################################################################
from __future__ import unicode_literals

from django.db import migrations, models

def do_stuff(apps, schemd_editor) -> None:
    Char_val = apps.get_model('calligraphy', 'Character').objects
    Char_ori = apps.get_model('calligraphy', 'Character').objects
    Pages = apps.get_model('calligraphy', 'Page').objects
    DetectedBox = apps.get_model('calligraphy', 'DetectedBox').objects
    PagesHaveChars = apps.get_model('calligraphy', 'PagesHaveChars').objects
    for thisPage in PagesHaveChars.all():
        print("--------------------------------------------------------------------")
        print("PageId: " + str(thisPage.haveChars.id))
        black_features = 0
        black_features_valid = 0
        white_features = 0
        white_features_valid = 0
        black_pixels = 0
        white_pixels = 0
        curPage = Pages.get(id=thisPage.haveChars.id)
        features  = DetectedBox.filter(parent_page=curPage)
        for feature in features:
            if feature.black_chars:
                black_features += 1
                if feature.percent_inside_currated_box >= 80:
                    black_features_valid += 1
                    black_pixels += feature.area
                    
            else:
                white_features += 1
                if feature.percent_inside_currated_box >= 80:
                    white_features_valid += 1
                    white_pixels += feature.area
        print("Black Features all: " + str(black_features))
        print("White Features all: " + str(white_features))
        print("Black Features vld: " + str(black_features_valid))
        print("White Features vld: " + str(white_features_valid))
        print("Black Features pxl: " + str(black_pixels))
        print("White Features pxl: " + str(white_pixels))
        if black_pixels > white_pixels:
            for feature in features:
                if feature.black_chars is False:
                    feature.delete()
        else:
            for feature in features:
                if feature.black_chars:
                    feature.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('calligraphy', '0126_auto_20170915_1425'),
    ]

    operations = [ migrations.RunPython(do_stuff)
    ]
