from .utils import cmd_exists, peerflix
import sys
import colorful
from .argument_parser import Parser
from .ezflix import Ezflix
from prettytable import PrettyTable
from builtins import input
import logging
import time
import subprocess
from .extractors.yts import find_similar

logging.basicConfig()
logger = logging.getLogger(__file__)

if sys.version_info < (2, 7):
    logger.error('You need Python 2.7 or later\n')
    sys.exit(1)

parser = Parser()

if len(sys.argv) == 1:
    parser.error()
    sys.exit(0)

args = parser.parse()

media_player = args.media_player
debug = args.debug

if not cmd_exists('peerflix'):
    sys.exit('This program requires Peerflix. https://github.com/mafintosh/peerflix')

if not cmd_exists('mpv') and args.media_player == 'mpv':
    logger.warning("In ezflix, the default player is mpv. It will fallback to vlc if mpv isn't found. "
                   "You can use the media_player argument to set your media player.")
    media_player = 'vlc'

if not (args.query and args.query.strip()):
    sys.exit(colorful.red("Search query not valid."))

def display_menu():
    print(colorful.bold("Make selection (Select the ID of the media you want to stream):"))
    print("Enter 'quit' to close the program.")
    print("Enter 'next' to see the next page of movies.")
    print("Enter 'prev' to see the previous page of movies.")
    print("Enter 'search' to refine your search.")
    print("Enter 'info' and the id of the torrent to get the movie/tv show overview.")
    print("Enter 'trailer' and the id of the torrent to play the movie trailer.")
    print("Enter 'similar' and the id of the torrent to find similar movies.")
    
def display_table(torrents):
    row = PrettyTable()
    row.field_names = ["Id", "Torrent", "Seeds", "Peers", "Released", "Rating"]
    row.align = 'l'
    t = 0
    for result in torrents:
        if not (result['seeds'] == 0 or result['seeds'] is None) or args.no_seeds is True:
            row.add_row([result['id'], result['title'], result['seeds'], result['peers'], result['release_date'], result['rating']])
            t += 1
    if t > 0 or args.no_seeds:
        print(row)
    else:
        print(colorful.red("No results found."))
        sys.exit(0)

def search(page=1, term=None):
    global ezflix
    ezflix = Ezflix(query=args.query if term is None else term,
                    media_type=args.media_type,
                    limit=int(args.limit),
                    sort_by=args.sort_by,
                    sort_order=args.sort_order,
                    quality=args.quality,
                    minimum_rating=args.minimum_rating,
                    language=args.language,
                    page=page,
                    debug=debug
                    )
    torrents = ezflix.search()
    if torrents is None or len(torrents) == 0:
        sys.exit(colorful.red('No results found.'))
    if args.latest:
        latest = torrents[0]
        file_path = ezflix.search_subtitles(latest['title']) if args.subtitles else None
        print("Playing " + latest['title'])
        time.sleep(2.5)
        peerflix(latest['magnet'], media_player, args.media_type, args.subtitles, args.remove, file_path)
        sys.exit()
    display_table(torrents)
    
def main():
    page = 1
    search(page)
    if not args.no_menu:
        display_menu()
    while True:
        read = input()
        if read == 'quit':
            sys.exit()
        elif read == 'search':
            refined_query = input("Enter search query: ")
            search(page=1, term=refined_query)
            continue
        elif read == 'next':
            page += 1
            search(page)
            continue
        elif read == 'prev':
            if page > 1:
                page -= 1
                search(page)
            continue
        elif 'info' in read:
            sp = read.split()
            if not len(sp) > 1:
                print(colorful.bold(
                    "Incorrect usage. Enter 'info' and the id of the torrent to get the movie/tv show overview."))
                continue
            magnet = ezflix.magnet(sp[1])
            if magnet is None:
                print(colorful.red("Invalid selection."))
                continue
            print(magnet['overview'])
            continue
        elif 'similar' in read:
            sp = read.split()
            if not len(sp) > 1:
                print(colorful.bold(
                    "Incorrect usage. Enter 'similar' and the id of the torrent to find similar movies."))
                continue
            magnet = ezflix.magnet(sp[1])
            if magnet is None:
                print(colorful.red("Invalid selection."))
                continue
            torrents = find_similar(magnet['yts_id'])
            display_table(torrents)
            continue
        elif 'trailer' in read:
            sp = read.split()
            if not len(sp) > 1:
                print(colorful.red(
                    "Incorrect usage. Enter 'trailer' and the id of the torrent to play the movie trailer."))
                continue
            magnet = ezflix.magnet(sp[1])
            if magnet is None:
                print(colorful.red("Invalid selection."))
                continue
            trailer = magnet['trailer']
            if not trailer:
                print(colorful.red("No trailer found."))
                continue
            geometry = "50%"
            subprocess.Popen(
                ['/bin/bash', '-c', "%s https://www.youtube.com/watch?v=%s --geometry=%s" % (media_player, trailer, geometry)])
            continue
        try:
            int_val = int(read)
        except (ValueError, TypeError):
            print(colorful.red('Invalid selection.'))
            continue
        magnet = ezflix.magnet(int_val)
        if not magnet:
            print(colorful.red('Invalid selection.'))
            continue
        print("Playing " + magnet['title'])
        file_path = ezflix.search_subtitles(magnet['title']) if args.subtitles else None
        peerflix(magnet['magnet'], media_player, args.media_type, args.subtitles, args.remove, file_path)
        sys.exit()


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        sys.exit(1)
