import json
import storj
from PyQt4 import QtCore, QtGui
from mainUI import MainUI
from qt_interfaces.storj_login_ui import Ui_Login
from utilities.account_manager import AccountManager


# Login section
class LoginUI(QtGui.QMainWindow):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        # login UI
        self.login_ui = Ui_Login()
        self.login_ui.setupUi(self)

        # Account manager
        self.login_ui.password.setEchoMode(QtGui.QLineEdit.Password)

        QtCore.QObject.connect(self.login_ui.login_bt, QtCore.SIGNAL("clicked()"), self.login)  # take login action

    def login(self):
        # take login action
        self.email = self.login_ui.email.text()  # get login
        self.password = self.login_ui.password.text()  # get password

        self.storj_client = storj.Client(email=str(self.email).strip(),
                                         password=str(self.password).strip())
        success = False
        # take login action - check credentials by listing keys :D
        try:
            self.storj_client.key_list()
            success = True
        except storj.exception.StorjBridgeApiError as e:
            j = json.loads(str(e))
            if j["error"] == "Invalid email or password":
                QMessageBox.about(self, "Warning",
                                  "Invalid email or password - access denied. Please check your credentials and try again!")
            else:
                QMessageBox.about(self, "Unhandled exception", "Exception: " + str(e))

        if success:
            self.account_manager = AccountManager(str(self.email), str(self.password))  # init account manager
            self.account_manager.save_account_credentials()  # save login credentials and state
            msgBox = QtGui.QMessageBox(QtGui.QMessageBox.Information, "Success", "Successfully loged in!",
                                       QtGui.QMessageBox.Ok)
            result = msgBox.exec_()
            if result == QtGui.QMessageBox.Ok:
                self.main_ui_window = MainUI(self)
                self.main_ui_window.show()
                self.close()
                #initial_window.hide()

                # self.account_manager.get_login_state()

        # print self.storj_client.bucket_list()
        print 1

@@ -689,16 +689,20 @@
 email = 
+str(
 self.log
@@ -719,24 +719,33 @@
 l.text()
+).strip()
   # get 
 login%0A  
@@ -736,21 +736,20 @@
   # get 
-login
+mail
 %0A       
@@ -765,16 +765,20 @@
 sword = 
+str(
 self.log
@@ -798,16 +798,25 @@
 d.text()
+).strip()
   # get 
@@ -872,20 +872,16 @@
 t(email=
-str(
 self.ema
@@ -882,25 +882,16 @@
 lf.email
-).strip()
 ,%0A      
@@ -934,20 +934,16 @@
 assword=
-str(
 self.pas
@@ -952,17 +952,8 @@
 ord)
-.strip())
 %0A   
@@ -1233,17 +1233,21 @@
 if j
-%5B
+.get(
 %22error%22
-%5D
+)
  == 
@@ -1283,32 +1283,38 @@
                 
+QtGui.
 QMessageBox.abou
@@ -1332,16 +1332,22 @@
 rning%22,%0A
+      
         
@@ -1416,16 +1416,59 @@
 denied. 
+%22%0A                                        %22
 Please c
@@ -1540,16 +1540,22 @@
         
+QtGui.
 QMessage
@@ -1688,20 +1688,16 @@
 Manager(
-str(
 self.ema
@@ -1702,15 +1702,10 @@
 mail
-)
 , 
-str(
 self
@@ -1714,17 +1714,16 @@
 assword)
-)
   # init

improve arguments retrieval of login