# -*- coding: utf-8 -*-
#
# Copyright 2016 Continuum Analytics, Inc.
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
#

"""
"""

# Standard library imports
import sys

# Third party imports
from qtpy.QtCore import Qt
from qtpy.QtWidgets import (QComboBox, QDialogButtonBox, QGridLayout,
                            QLabel, QLineEdit, QPushButton, QVBoxLayout)

# Local imports
from anaconda_navigator.widgets import ButtonCancel
from anaconda_navigator.widgets.dialogs import DialogBase


class CreateEnvironmentDialog(DialogBase):
    def __init__(self, parent=None, path=None, environments=None):
        super(CreateEnvironmentDialog, self).__init__(parent=parent)

        self.environments = environments

        # Widgets
        self.label_name = QLabel("Environment name")
        self.label_version = QLabel("Python version")
        self.text_name = QLineEdit()
        self.combo_version = QComboBox()
        self.button_ok = QPushButton('Ok')
        self.button_cancel = ButtonCancel('Cancel')
        self.buttonbox = QDialogButtonBox(Qt.Horizontal)

        # Widgets setup
        self.text_name.setPlaceholderText("New environment name")
        self.setMinimumWidth(300)
        self.setWindowTitle("Create new environment")

        # TODO: Possible set of python versions?
        versions = ['2.7', '3.4', '3.5']
        now = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
        self.combo_version.addItems(versions)
        self.combo_version.setCurrentIndex(versions.index(now))
        self.buttonbox.addButton(self.button_ok, QDialogButtonBox.ActionRole)
        self.buttonbox.addButton(self.button_cancel,
                                 QDialogButtonBox.ActionRole)

        # Layouts
        grid = QGridLayout()
        grid.addWidget(self.label_name, 0, 0)
        grid.addWidget(self.text_name, 0, 1)
        grid.addWidget(self.label_version, 1, 0)
        grid.addWidget(self.combo_version, 1, 1)

        main_layout = QVBoxLayout()
        main_layout.addLayout(grid)
        main_layout.addSpacing(14)
        main_layout.addWidget(self.buttonbox)

        self.setLayout(main_layout)

        # Signals
        self.button_ok.clicked.connect(self.accept)
        self.button_cancel.clicked.connect(self.reject)
        self.text_name.textChanged.connect(self.refresh)

        # Setup
        self.refresh()

    def refresh(self, text=''):
        text = text.strip()
        if self.environments:
            if text and text not in self.environments:
                self.button_ok.setDisabled(False)
            else:
                self.button_ok.setDisabled(True)


class CloneEnvironmentDialog(CreateEnvironmentDialog):
    def __init__(self, parent=None, path=None, environments=None):
        super(CloneEnvironmentDialog, self).__init__(parent=parent,
                                                     path=path,
                                                     environments=environments)
        self.setWindowTitle('Clone environment')
        self.combo_version.setVisible(False)
        self.label_version.setVisible(False)


class RemoveEnvironmentDialog(DialogBase):
    def __init__(self, parent=None, environment=None):
        super(RemoveEnvironmentDialog, self).__init__(parent=parent)

        self.buttonbox = QDialogButtonBox(Qt.Horizontal)
        self.button_cancel = ButtonCancel('Cancel')
        self.button_ok = QPushButton('Ok')
        self.label_environment = QLabel('Do you want to remove environment '
                                        '<b>"{0}"</b>'.format(environment))
        self.setWindowTitle('Remove environment')
        self.buttonbox.addButton(self.button_ok, QDialogButtonBox.ActionRole)
        self.buttonbox.addButton(self.button_cancel,
                                 QDialogButtonBox.ActionRole)

        # Layouts
        layout = QVBoxLayout()
        layout .addWidget(self.label_environment)
        layout.addSpacing(14)
        layout.addWidget(self.buttonbox)

        self.setLayout(layout)

        # Signals
        self.button_ok.clicked.connect(self.accept)
        self.button_cancel.clicked.connect(self.reject)


def test():
    from anaconda_navigator.utils.qthelpers import qapplication
    app = qapplication()
    dlg = CreateEnvironmentDialog(parent=None)
    reply = dlg.exec_()
    print(reply)

    dlg = CloneEnvironmentDialog(parent=None)
    reply = dlg.exec_()
    print(reply)

    dlg = RemoveEnvironmentDialog(parent=None)
    reply = dlg.exec_()
    print(reply)


if __name__ == '__main__':
    test()

