import json
from os.path import join

from django.conf import settings
from django.core.management.base import NoArgsCommand

from program.models import ProgramSlot


class Command(NoArgsCommand):
    help = 'checks the automation_ids used by program slots against the exported'

    def handle_noargs(self, **options):
        cache_dir = getattr(settings, 'AUTOMATION_CACHE_DIR', 'cache')
        cached_shows = join(cache_dir, 'shows.json')
        with open(cached_shows) as shows_json:
            shows = json.loads(shows_json.read())['shows']

            automation_ids = []
            for show in shows:
                automation_ids.append(show['id'])
            automation_ids.sort()

            automation_ids2 = []
            for programslot in ProgramSlot.objects.filter(automation_id__isnull=False):
                automation_ids2.append(int(programslot.automation_id))
            automation_ids2.sort()

            for automation_id in automation_ids:
                if automation_id not in automation_ids2:
                    print '+', automation_id

            for automation_id in automation_ids2:
                if automation_id not in automation_ids:
                    print '-', automation_id

@@ -537,17 +537,8 @@
 d())
-%5B'shows'%5D
 %0A%0A  
@@ -551,27 +551,19 @@
     
-automation
+rd
 _ids = 
-%5B%5D
+%7B%7D
 %0A   
@@ -588,16 +588,25 @@
 in shows
+%5B'shows'%5D
 :%0A      
@@ -619,30 +619,15 @@
     
-automation_ids.append(
+rd_ids%5B
 show
@@ -632,17 +632,24 @@
 ow%5B'id'%5D
-)
+%5D = show
 %0A       
@@ -657,29 +657,83 @@
     
-automation_ids.sort()
+for show in shows%5B'multi-shows'%5D:%0A                rd_ids%5Bshow%5B'id'%5D%5D = show
 %0A%0A  
@@ -742,31 +742,22 @@
         
-automation
+pv
 _ids
-2
  = %5B%5D%0A  
@@ -858,31 +858,22 @@
         
-automation
+pv
 _ids
-2
 .append(
@@ -904,16 +904,17 @@
 on_id))%0A
+%0A
         
@@ -917,16 +917,20 @@
         
+for 
 automati
@@ -938,19 +938,43 @@
 n_id
-s2.sort()%0A%0A
+ in sorted(rd_ids.iterkeys()):%0A    
     
@@ -973,36 +973,42 @@
                 
-for 
+if rd_ids%5B
 automation_id in
@@ -996,36 +996,147 @@
 ds%5Bautomation_id
- in 
+%5D%5B'type'%5D == 's':%0A                    continue%0A%0A                multi_id = -1%0A                if 'multi' in rd_ids%5B
 automation_ids:%0A
@@ -1124,33 +1124,33 @@
 ds%5Bautomation_id
-s
+%5D
 :%0A              
@@ -1155,47 +1155,170 @@
     
-if automation_id not in automation_ids2
+    multi_id = rd_ids%5Bautomation_id%5D%5B'multi'%5D%5B'id'%5D%0A                if automation_id not in pv_ids and multi_id not in pv_ids:%0A                    if multi_id %3C 0
 :%0A  
@@ -1339,16 +1339,20 @@
     
+ 
+   
 print '+
 ', a
@@ -1347,19 +1347,24 @@
 print '+
-', 
+ %25d' %25 (
 automati
@@ -1372,58 +1372,154 @@
 n_id
-%0A%0A            for automation_id in automation_ids2
+)%0A                    else:%0A                        print '+ %25d (%25d)' %25 (automation_id, multi_id)%0A%0A            for automation_id in sorted(pv_ids)
 :%0A  
@@ -1556,26 +1556,18 @@
  not in 
-automation
+rd
 _ids:%0A  

fix check_automation_ids command