# coding: utf-8
import sys
import re
import logging
import webbrowser
from functools import partial

import sublime
from sublime_plugin import WindowCommand

from . import __version__
from .cmd import Cmd
from .util import noop, get_settings, SETTINGS_FILE


logger = logging.getLogger('SublimeGit.sublimegit')


SUBLIMEGIT_LICENSE_EXISTS = ("You already have a license installed. "
                             "Do you want to overwrite it?\n\n"
                             "Email:\n  %s\n"
                             "License key:\n  %s")


class SublimeGitInstallLicenseCommand(WindowCommand, Cmd):
    """
    Install a SublimeGit license.

    You will be asked to enter your email address and license key. If there
    is already a license installed, you will be asked if you want to overwrite
    it. Press escape at any time to cancel.

    .. note::

        This command only does basic validation on your license. If the
        evaluation popup keeps appearing, then there is an issue with your license,
        and you should contact support@sublimegit.net

    """

    def _show_email_input(self, initial):
        self.window.show_input_panel('Email:', initial, self.on_email, noop, noop)

    def _show_license_input(self, email, initial=''):
        self.window.show_input_panel('License Key:', initial, partial(self.on_key, email), noop, noop)

    def run(self):
        settings = get_settings()

        email = settings.get('email', '')
        key = settings.get('product_key')

        if email and key:
            msg = SUBLIMEGIT_LICENSE_EXISTS % (email, key)
            if not sublime.ok_cancel_dialog(msg, 'Overwrite'):
                return

        self._show_email_input(email)

    def on_email(self, email):
        email = email.strip()

        if not email:
            sublime.error_message("Please provide an email, or press escape to cancel.")
            self._show_email_input(email)
            return

        if '@' not in email:
            sublime.error_message("Are you sure that's a valid email?")
            self._show_email_input(email)
            return

        self._show_license_input(email)

    def on_key(self, email, key):
        key = key.strip()

        if not key:
            sublime.error_message("Please provide a license key, or press escape to cancel.")
            self._show_license_input(email)
            return

        if len(key) != 32 or re.search(r'[^0-9a-f]', key) is not None:
            sublime.error_message("Invalid key format. Please double check the license key.")
            self._show_license_input(email, key)
            return

        settings = get_settings()
        settings.set('email', email)
        settings.set('product_key', key)

        sublime.save_settings(SETTINGS_FILE)
        sublime.message_dialog("Thank you. Your license has been installed. Please restart Sublime Text.")


class SublimeGitVersionCommand(WindowCommand):
    """
    Show the currently installed version of SublimeGit.
    """

    def run(self):
        sublime.message_dialog("You have SublimeGit %s" % __version__)


class SublimeGitBuyLicenseCommand(WindowCommand):
    """
    Buy SublimeGit. We love you!
    """

    URL = "https://sublimegit.net/buy?utm_source=st%s&utm_medium=command&utm_campaign=buy"

    def run(self):
        url = self.URL % sys.version_info[0]
        webbrowser.open(url)


class SublimeGitDocumentationCommand(WindowCommand):
    """
    Open a webbrowser to the online SublimeGit documentation.
    """

    URL = "https://docs.sublimegit.net/?utm_source=st%s&utm_medium=command&utm_campaign=docs"

    def run(self):
        url = self.URL % sys.version_info[0]
        webbrowser.open(url)

@@ -181,29 +181,8 @@
 n__%0A
-from .cmd import Cmd%0A
 from
@@ -573,13 +573,8 @@
 mand
-, Cmd
 ):%0A 
@@ -1470,16 +1470,20 @@
 uct_key'
+, ''
 )%0A%0A     

Remove unneeded inheritance in license command