#!/usr/bin/python

# Audio Tools, a module and set of tools for manipulating audio data
# Copyright (C) 2007-2014  Brian Langenberger

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


import sys
import os.path
import audiotools
import audiotools.text as _

if (__name__ == '__main__'):
    import argparse

    parser = argparse.ArgumentParser(description=_.DESCRIPTION_DVDAINFO)

    parser.add_argument("--version",
                        action="version",
                        version="Python Audio Tools %s" % (audiotools.VERSION))

    parser.add_argument("-A", "--audio-ts",
                        default=".",
                        dest="audio_ts",
                        metavar="DIR",
                        help=_.OPT_AUDIO_TS)

    options = parser.parse_args()

    msg = audiotools.Messenger("dvdainfo", options)

    try:
        dvda = audiotools.DVDAudio(options.audio_ts)
    except audiotools.InvalidDVDA as err:
        msg.error(unicode(err))
        sys.exit(1)
    except OSError as err:
        msg.os_error(err)
        sys.exit(1)

    table = audiotools.output_table()

    titleset = dvda[0]
    for (title_num, title) in enumerate(titleset):
        (sample_rate,
         channels,
         channel_mask,
         bits_per_sample,
         stream_type) = title.info()

        row = table.row()
        row.add_column(_.LAB_DVDA_TITLE % (title_num + 1), "right")
        row.add_column(_.LAB_DVDA_TRACKS % (len(title.tracks)), "right")
        row.add_column(u" : ")
        row.add_column(_.LAB_DVDA_TITLE_INFO %
                       {"minutes": title.pts_length // 90000 // 60,
                        "seconds": title.pts_length // 90000 % 60,
                        "channels": channels,
                        "rate": audiotools.khz(sample_rate),
                        "bits": bits_per_sample,
                        "type": {0xA0: u"PCM",
                                 0xA1: u"MLP"}.get(stream_type,
                                                   u"UNKNOWN")})
    for row in table.format(msg.output_isatty()):
        msg.output(row)

    msg.output(u"")

    table = audiotools.output_table()

    row = table.row()
    row.add_column(_.LAB_DVDAINFO_TITLE)
    row.add_column(u" ")
    row.add_column(_.LAB_DVDAINFO_TRACK)
    row.add_column(u" ")
    row.add_column(_.LAB_DVDAINFO_LENGTH)
    row.add_column(u"   ")
    row.add_column(_.LAB_DVDAINFO_FILENAME)
    row.add_column(u" ")
    row.add_column(_.LAB_DVDAINFO_STARTSECTOR)
    row.add_column(u" ")
    row.add_column(_.LAB_DVDAINFO_ENDSECTOR)
    row.add_column(u" ")
    row.add_column(_.LAB_DVDAINFO_TICKS)

    table.divider_row([_.DIV, u" ", _.DIV, u" ", _.DIV, u" ", _.DIV,
                       u" ", _.DIV, u" ", _.DIV, u" ", _.DIV])

    for (title_num, title) in enumerate(titleset):
        for (track_num, track) in enumerate(title.tracks):
            for (i, (aob_file,
                     start_sector,
                     end_sector)) in enumerate(track.sectors()):
                row = table.row()
                if (i == 0):
                    row.add_column(unicode(title_num + 1), "right")
                    row.add_column(u" ")
                    row.add_column(unicode(track_num + 1), "right")
                    row.add_column(u" ")
                    row.add_column(_.LAB_TRACK_LENGTH %
                                   (track.pts_length // 90000 // 60,
                                    track.pts_length // 90000 % 60),
                                   "right")
                else:
                    row.add_column(u"")
                    row.add_column(u" ")
                    row.add_column(u"")
                    row.add_column(u" ")
                    row.add_column(u"")
                row.add_column(u"   ")
                row.add_column(
                    unicode(audiotools.Filename(os.path.basename(aob_file))),
                    "right")
                row.add_column(u" ")
                row.add_column(unicode(start_sector), "right")
                row.add_column(u" ")
                row.add_column(unicode(end_sector - 1), "right")
                row.add_column(u" ")
                if (i == 0):
                    row.add_column(unicode(track.pts_length), "right")
                else:
                    row.add_column(u" ")

    for row in table.format(msg.output_isatty()):
        msg.output(row)
