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

from __future__ import division, absolute_import
from PIL import Image
import numpy as np
from pylab import imshow, savefig  # , show, get_cmap
from tools import Worker, Isovist, TrackCollection, IndoorToolset  # , Track, IsovistCollection

# import tensorflow as tf


if __name__ == '__main__':
    walkableTiles = 255
    w = Worker()
    # file = 'maps\Tate.bmp'
    # file = 'maps\Map.bmp'
    # file = 'maps\Maze.bmp'
    # file = 'maps\oet.bmp'
    # file = 'maps\doom.bmp'
    # file = 'maps\priz.bmp'
    # file = 'maps\\tum.bmp'
    file = 'maps\crossing.bmp'
    with Image.open(file) as f:
        baseToolset = IndoorToolset(np.array(f), walkableTiles, worker=w, isoVistSize=30)

    baseToolset.refresh_random_clock()

    """HEATMAP, Random"""
    # Heatmap with random starts/targets
    if False:
        pCol = TrackCollection(baseToolset, worker=w)
        pCol.add_n_bunch_random(100, penalty=2, safe=True)
        pCol.show(graphUpdate=True, saveIMG=True)

    """Homotroph Analysis"""
    # Homotrphic Colored Pixels with same Starts/Targets, vertices iterating over all walkable pixels.
    if False:
        st = baseToolset.getRandomPos()
        baseToolset.refresh_random_clock()
        ta = baseToolset.getRandomPos()
        pCol = TrackCollection(baseToolset)

        pCol.add_n_bunch_tracks('all', st, ta)

        pCol.homotopic_classification()
        pCol.show(hClass=True, saveIMG=True)

    """Principal Convex Hull Analysis (PCHA) / Archetypal Analysis."""
    # refer to https://github.com/ulfaslak/py_pcha for further Information and original Editor
    if False:
        if True:
            st = baseToolset.getRandomPos()
            baseToolset.refresh_random_clock()
            ta = baseToolset.getRandomPos()
        if False:                       # NOTE: use this with Maze.bmp!!!!!!
            st = (98, 6)
            ta = (98, 195)
        if False:                        # NOTE: use this with Map.bmp!!!!!!
            st = (15, 15)
            ta = (190, 280)
        pCol = TrackCollection(baseToolset)
        # ToDo: Print the k value as chart
        # noinspection PyUnboundLocalVariable
        pCol.add_n_bunch_tracks(20, st, ta, penalty=2)
        pCol.homotopic_classification()
        if False:
            ArchetypeList = pCol.return_archetypes(2)
            pCol.show(saveIMG=True, trackList=ArchetypeList, allTracks=True)
            ArchetypeList = pCol.return_archetypes(4)
            pCol.show(saveIMG=True, trackList=ArchetypeList, allTracks=True)
            ArchetypeList = pCol.return_archetypes(5)
            pCol.show(saveIMG=True, trackList=ArchetypeList, allTracks=True)
        ArchetypeList = pCol.return_archetypes(3)
        pCol.show(saveIMG=True, trackList=ArchetypeList, allTracks=True)

    """Isovists Testing"""
    # Some Explanation text here
    if False:
        point = baseToolset.getRandomPos()
        print(point)
        i = Isovist(*point, array=baseToolset.imgArray, walkable=walkableTiles, rangeLimit=30)
        i.saveImg()
        baseToolset.imgArray[point] = 160
        imshow(baseToolset.imgArray)
        savefig('startpos.tif', interpolation='none', cmap='gray')

    """Synthesyse n-bunch Tracks/ minimum length / storage for TensorFlow"""
    # Some Explenation text here
    if True:
        pCol = TrackCollection(baseToolset)
        pCol.add_single_track((67, 103), (67, 64), qhull=False)
        pCol.add_single_track((47, 82), (88, 82), qhull=False)
        #pCol.add_n_bunch_random(
        #    1000, penalty=None, safe=False,
        #    minLen=50
        #    # minLen=int(sqrt(baseToolset.width * baseToolset.height) / 4)
        #)

        pCol.save_to_disc('crossing')

        # pCol3 = TrackCollection(baseToolset)
        # pCol3.recover_from_disc('synthetic_tracks_sizeDIV4')

    print('Success!')
    pass
    pass
