#!/usr/bin/env python
#### Jade Application Kit
# * https://codesardine.github.io/Jade-Application-Kit
# * Vitor Lopes Copyright (c) 2016 - 2020
# * https://vitorlopes.me
import argparse
import os
import sys
import subprocess
"""
 App Name   - Jade Application Kit
 App Url    - https://codesardine.github.io/Jade-Application-Kit
 Author     - Vitor Lopes -> Copyright (c) 2016 - 2020
 Author Url - https://vitorlopes.me
"""
from JAK.Application import JWebApp


def run(url, title,  online,  transparent, debug, disable_gpu, icon, cookies_path, user_agent, custom_css, custom_js):
    app = JWebApp(
                  debug=debug,
                  disableGPU=disable_gpu,
                  window={
                     "title": title,
                     "icon": icon,
                     "transparent": transparent,
                     "showHelpMenu": True,
                   },
                  webview={
                     "webContents": url,
                     "addCSS": custom_css,
                     "cookiesPath": cookies_path,
                     "online": online,
                     "userAgent": user_agent,
                     "runJavaScript": custom_js
                  })
    app.run()


def command_line_options():
    """Parse commandline arguments."""
    options = argparse.ArgumentParser(description='''\
      -------------------------------------------------------
      Jade Application Kit
      -------------------------------------------------------
      Toggle Full Screen    [  F11     ]
      Zoom In               [  CTRL +  ]
      Zoom Out              [  CTRL -  ]
      -------------------------------------------------------
      Create hybrid desktop applications
      with Python, JavaScript or Shell
      Author : Vitor Lopes
      Licence: GPL
      url: https://codesardine.github.io/Jade-Application-Kit
      -------------------------------------------------------''', epilog='''\
      ''', formatter_class=argparse.RawTextHelpFormatter)

    options.add_argument(
        "--url",
        type=str,
        required=True,
        help="Url or path to HTML file."
    )
    options.add_argument(
        "--title",
        type=str,
        help="Application Title.",
        required=True
    )
    options.add_argument(
        "--icon",
        default="applications-internet",
        type=str,
        help="Path to app icon, if omitted, uses url icon.",
    )
    options.add_argument(
        "--transparent",
        default=False,
        type=bool,
        help="Transparent Background.",
    )
    options.add_argument(
        "--online",
        default=False,
        type=bool,
        help="cache/cookies/dns prefetch ON.",
    )
    options.add_argument(
        "--cookies_path",
        default="",
        type=str,
        help="Set path, /home/username/.jak/cookies-path.",
    )
    options.add_argument(
        "--user_agent",
        default="",
        type=str,
        help="Set WebEngine user agent",
    )
    options.add_argument(
        "--css",
        default="",
        type=str,
        help='Inject CSS, file path or string.',
    )
    options.add_argument(
        "--js",
        default="",
        type=str,
        help='Inject JavaScript, file path or string.',
    )
    options.add_argument(
        "--dev",
        default=False,
        action='store_true',
        help="Debug On",
    )
    options.add_argument(
        "--cde",
        default=False,
        action='store_true',
        help="Create Desktop Entry",
    )
    options.add_argument(
        "--desc",
        type=str,
        help="Desktop Entry Application Description"
    )
    options.add_argument(
        "--disable-gpu",
        default=False,
        action='store_true',
        help="Disable GPU acceleration, use this if you have a NVIDIA card",
    )
    # Todo pass dictionary as argument, this one is tricky
    """
    options.add_argument(
        "--toolbar",
        default="",
        type=dict,
        help='Define toolbar links, set in quotes -> "CSS"',
    )
    options.add_argument(
        "--url_rules",
        default="",
        help="if online, allowed url's.",
    )
    """

    return options.parse_args()


if len(sys.argv) == 1:
    # if no arguments are passed run help message
    process = subprocess.Popen([os.path.realpath(__file__), "--help"])
    process.wait()
    process.terminate()

option = command_line_options()

if option.url and option.title:
    if option.cde:
        if option.desc:
            from JAK.Utils import create_desktop_entry as cde
            cde(option.url, option.title, option.desc, option.icon)
        else:
            print("Description required\n --desc 'My description'")
    else:
        run(option.url, option.title, option.online, option.transparent, option.dev, option.disable_gpu, option.icon,
            option.cookies_path, option.user_agent, option.css, option.js)

