#!/usr/bin/env python
# This source file is part of Soundcloud-syncer.
#
# Soundcloud-syncer 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 3 of the License, or
# (at your option) any later version.
#
# Soundcloud-syncer 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
# Soundcloud-syncer. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.

import argparse
import os.path

from ssyncer.sclient import sclient
from ssyncer.suser import suser
from ssyncer.serror import serror

def downloader(tgetter, output, process_tag, *args):
    """ Download tracks. Return number of tracks downloaded. """
    try:
        tracks = tgetter(*args)
    except serror as e:
        print(e)
        return False

    for strack in tracks:
        try:
            strack.download(output, process_tag=process_tag)
        except serror as e:
            print(e)

    return len(tracks)

def main():
    parser = argparse.ArgumentParser(description="Soundcloud Syncer")
    parser.add_argument("-u", "--user", help="Soundcloud user to sync", type=str, required=True)
    parser.add_argument("-c", "--client-id", help="Your client id", type=str)
    parser.add_argument("-o", "--output-dir", help="Output directory", type=str, required=True)
    parser.add_argument("-O", "--offset", help="Tracks offset", type=int)
    parser.add_argument("-L", "--limit", help="Tracks limit (max: 200)", type=int)
    parser.add_argument("-r", "--recursive", help="Recursive download", action="store_true")
    parser.add_argument("-t", "--tracks", help="Download user's tracks instead user's likes", action="store_true")
    parser.add_argument("-w", "--without-tag", help="Download track without tags support", action="store_true")

    args = parser.parse_args()

    if not os.path.exists(args.output_dir):
        print(serror("Error: output directory `%s` doesn's exists." %
                     args.output_dir))
        exit(1)

    process_tag = False if args.without_tag else True

    offset = 0
    limit = 50
    if args.offset:
        offset = args.offset
    if args.limit:
        if args.limit > 200:
            print(serror("Error: tracks limit limited to 200 tracks.."))
            exit(2)
        limit = args.limit

    try:
        client = sclient(args.client_id)
        user = suser(args.user, client=client)
    except serror as e:
        print(e)
        exit(3)

    tracks_getter = user.get_likes
    if args.tracks:
        tracks_getter = user.get_tracks

    if args.recursive:
        while True:
            nb = downloader(tracks_getter, args.output_dir, process_tag, offset, limit)
            if nb == 0:
                break

            offset = offset + limit
    else:
        downloader(tracks_getter, args.output_dir, process_tag, offset, limit)

if __name__ == '__main__':
    main()
