# -*- coding: utf-8 -*-
from celery import group, shared_task

from os import listdir
from datetime import date, timedelta

from django.contrib.gis.geos import Polygon

from .models import Scene, Image, ScheduledDownload
from .utils import calendar_date, get_bounds, get_cloud_rate


@shared_task
def download_all():
    """Download all new Scenes of ScheduledDownloads."""
    for sd in ScheduledDownload.objects.all():
        sd.download_new_scene()
        sd.check_last_scene()


@shared_task
def process_scene(scene):
    return scene.process()


@shared_task
def process_all():
    """Process all scenes that have status 'downloaded'."""
    scenes = Scene.objects.filter(status='downloaded')
    group(process_scene.s(scene) for scene in scenes)().get()


def inspect_dir(dir, status='processed'):
    """Create a Scene using the name of the dir and list all TIF files present
    in that dir to create the Image objects in the database. If the Scene already
    exists, only create the missing Image objects.
    """
    scene_name = dir.split('/')[-1]

    #get cloud_rate of the scene
    try:
        cloud_rate = get_cloud_rate(scene_name)
    except FileNotFoundError:
        cloud_rate = None

    try:
        scene = Scene.objects.get(name=scene_name)
        if scene.cloud_rate is None:
            scene.cloud_rate = cloud_rate
            scene.save()

        print('%s already exists.' % scene_name)

    except Scene.DoesNotExist:
        #get geom of the Scene
        try:
            geom = Polygon(get_bounds(scene_name))
        except IndexError:
            geom = None

        scene = Scene.objects.create(
            sat='L' + scene_name[2],
            path=scene_name[3:6],
            row=scene_name[6:9],
            date=calendar_date(scene_name[9:13], scene_name[13:16]),
            geom=geom,
            cloud_rate=cloud_rate,
            name=scene_name,
            status=status
            )

    for image in listdir(dir):
        if image.endswith('.TIF') or image.endswith('.tif'):
            Image.objects.get_or_create(
                name=image,
                type=image.split('_')[1].split('.')[0],
                scene=scene
                )


def delete_unneeded_bands(bands=['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8',
    'B9', 'B10', 'B11', 'ndvi']):
    """Delete images of bands B1 to B11 and NDVI of scenes older than 32 days.
    """

    images = Image.objects.filter(type__in=bands,
        scene__date__lt=date.today() - timedelta(days=32))
    num = images.count()
    images.delete()
    print('%s images deleted.' % num)

@@ -285,24 +285,35 @@
 @shared_task
+(bind=True)
 %0Adef downloa
@@ -314,24 +314,28 @@
 ownload_all(
+self
 ):%0A    %22%22%22Do
@@ -378,24 +378,37 @@
 wnloads.%22%22%22%0A
+    try:%0A    
     for sd i
@@ -450,16 +450,20 @@
         
+    
 sd.downl
@@ -486,16 +486,20 @@
         
+ 
+   
 sd.check
@@ -512,16 +512,67 @@
 scene()%0A
+    except:%0A        raise self.retry(countdown=10)%0A
 %0A%0A@share
@@ -839,14 +839,8 @@
 s)()
-.get()
 %0A%0A%0Ad

add retry to download_all task