from .BaseIndexEntry import BaseIndexEntry


class AlbumIndexEntry(BaseIndexEntry):

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

        self._title_numbers = []
        self._discs_and_tracks = {}

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

            # Append the title index to the list
            self._title_numbers.append(title.index)

            # Store titles according to disc and track number
            if title.discnumber not in self._discs_and_tracks:
                self._discs_and_tracks[title.discnumber] = {}
            if title.tracknumber in self._discs_and_tracks[title.discnumber]:
                print ("Duplicate track number", title.tracknumber, title.title)
            self._discs_and_tracks[title.discnumber][title.tracknumber] = title

        self._freeze()

    # Getters

    @property
    def title_numbers(self):
        return self._title_numbers

    @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])]

@@ -197,41 +197,8 @@
 r)%0A%0A
-        self._title_numbers = %5B%5D%0A
     
@@ -387,151 +387,141 @@
   # 
-Append the title index to the list%0A            self._title_numbers.append(title.index)%0A%0A            # Store titles according to disc and track 
+Store titles according to disc and track number%0A            # TODO: Cope with more than two discs%0A            discnumber = title.disc
 numb
@@ -538,22 +538,16 @@
      if 
-title.
 discnumb
@@ -611,38 +611,32 @@
 iscs_and_tracks%5B
-title.
 discnumber%5D = %7B%7D
@@ -687,38 +687,32 @@
 iscs_and_tracks%5B
-title.
 discnumber%5D:%0A   
@@ -758,84 +758,536 @@
 mber
-%22, title.tracknumber, title.title)%0A            self._discs_and_tracks%5Btitle.
+s:%22)%0A                print (%22%5CtFirst%22, title.tracknumber, self._discs_and_tracks%5Bdiscnumber%5D%5Btitle.tracknumber%5D.title)%0A                print (%22%5CtSecond%22, title.tracknumber, title.title)%0A                discnumber = title.discnumber + 1%0A                if discnumber not in self._discs_and_tracks:%0A                    self._discs_and_tracks%5Bdiscnumber%5D = %7B%7D%0A                print (%22%5CtSetting disc number to: %7B%7D - you may want to edit the file and set disc number yourself.%22.format(discnumber))%0A            self._discs_and_tracks%5B
 disc
@@ -1423,27 +1423,127 @@
 urn 
+%5B
 self._
-title_numbers
+discs_and_tracks%5Bd%5D%5Bt%5D.index for d in sorted(self._discs_and_tracks) for t in sorted(self._discs_and_tracks%5Bd%5D)%5D
 %0A%0A  

Return title numbers in disc and track order, increment disc number if duplicated track number