from .BaseIndexEntry import BaseIndexEntry


class AlbumIndexEntry(BaseIndexEntry):

    def __init__(self, name, titles, number):
        super(AlbumIndexEntry, self).__init__(name, titles, number)

        self._discs_and_tracks = {}

        for title in self._titles:
            # Set the album number on each of the titles
            title.album_number = self._number

            # Store titles according to disc and track number
            # TODO: Cope with more than two discs
            discnumber = title.discnumber
            if discnumber not in self._discs_and_tracks:
                self._discs_and_tracks[discnumber] = {}
            if title.tracknumber in self._discs_and_tracks[discnumber]:
                print ("Duplicate track numbers:")
                print ("\tFirst", title.tracknumber, self._discs_and_tracks[discnumber][title.tracknumber].title)
                print ("\tSecond", title.tracknumber, title.title)
                discnumber = title.discnumber + 1
                if discnumber not in self._discs_and_tracks:
                    self._discs_and_tracks[discnumber] = {}
                print ("\tSetting disc number to: {} - you may want to edit the file and set disc number yourself.".format(discnumber))
            self._discs_and_tracks[discnumber][title.tracknumber] = title

        self._freeze()

    # Getters

    @property
    def title_numbers(self):
        return [self._discs_and_tracks[d][t].index for d in sorted(self._discs_and_tracks) for t in sorted(self._discs_and_tracks[d])]

    @property
    def tracks(self):
        '''Return titles in album disc and track order'''
        return [self._discs_and_tracks[d][t] for d in sorted(self._discs_and_tracks) for t in sorted(self._discs_and_tracks[d])]

@@ -447,88 +447,111 @@
     
-# TODO: Cope with more than two discs%0A            discnumber = title.discnumber%0A
+discnumber = title.discnumber%0A            new_location = False%0A            while not new_location:%0A    
     
@@ -611,32 +611,36 @@
                 
+    
 self._discs_and_
@@ -655,32 +655,102 @@
 iscnumber%5D = %7B%7D%0A
+                    new_location = True%0A                    break%0A    
             if t
@@ -813,32 +813,36 @@
                 
+    
 print (%22Duplicat
@@ -868,32 +868,36 @@
                 
+    
 print (%22%5CtFirst%22
@@ -895,37 +895,159 @@
 %22%5CtFirst
-%22, title.tracknumber,
+: Disc %7B%7D, Track %7B%7D, Title '%7B%7D'%22.format(%0A                        discnumber,%0A                        title.tracknumber,%0A                       
  self._d
@@ -1095,25 +1095,30 @@
 mber%5D.title)
-%0A
+)%0A    
             
@@ -1141,43 +1141,50 @@
 cond
-%22, title.tracknumber, t
+: Disc %7B%7D, Track %7B%7D, T
 itle
-.title)%0A
+ '%7B%7D'%22.format(%0A 
     
@@ -1187,32 +1187,39 @@
                 
+       
 discnumber = tit
@@ -1216,31 +1216,52 @@
 mber
+,%0A                      
  
-=
  title.
-disc
+track
 number
- + 1
+,
 %0A   
@@ -1277,113 +1277,70 @@
     
-if discnumber not in self._discs_and_tracks:%0A                    self._discs_and_tracks%5Bdiscnumber%5D = %7B%7D%0A
+        title.title))%0A                    discnumber += 1%0A    
     
@@ -1362,120 +1362,109 @@
 nt (
-%22%5CtSett
+'%5CtTry
 ing dis
-c number to: %7B%7D - you may want to edit the file and set disc number yourself.%22.format(discnumber))
+k %7B%7D'.format(discnumber))%0A                else:%0A                    new_location = True%0A
 %0A   

Increment discnumber until no clash.