import json
import socket
from PyQt4 import QtCore, QtGui

from qt_interfaces.node_details_ui import Ui_NodeDetails
from engine import StorjEngine
from utilities.tools import Tools

from ipwhois import IPWhois
import pycountry
import threading
import storj.exception as sjexc
from utilities.log_manager import logger

from resources.html_strings import html_format_begin, html_format_end


# Node details section
class NodeDetailsUI(QtGui.QMainWindow):

    def __init__(self, parent=None, nodeid=None):
        QtGui.QWidget.__init__(self, parent)
        self.storj_engine = StorjEngine()  # init StorjEngine
        # login UI
        self.node_details_ui = Ui_NodeDetails()
        self.node_details_ui.setupUi(self)

        self.nodeid = nodeid
        self.tools = Tools()

        QtCore.QObject.connect(self.node_details_ui.ok_bt, QtCore.SIGNAL("clicked()"), self.close)  # close window
        self.connect(self, QtCore.SIGNAL("showBridgeExceptionMessageBox"), self.show_storj_bridge_exception)

        self.createNewNodeDetailsResolveThread()

    def show_storj_bridge_exception(self, exception_content):
        try:
            j = json.loads(str(exception_content))
            QtGui.QMessageBox.critical(self, "Bridge error", str(j["error"]))
        except Exception as e:
            QtGui.QMessageBox.critical(self, "Bridge error", str(exception_content))
            logger.error(e)

    def createNewNodeDetailsResolveThread(self):
        download_thread = threading.Thread(target=self.initialize_node_details, args=())
        download_thread.start()

    def initialize_node_details(self):

        try:
            self.node_details_content = self.storj_engine.storj_client.contact_lookup(str(self.nodeid))

            self.node_details_ui.address_label.setText(
                html_format_begin + str(self.node_details_content.address) + html_format_end)  # get given node address
            self.node_details_ui.last_timeout_label.setText(
                html_format_begin + str(self.node_details_content.lastTimeout) + html_format_end)  # get last timeout
            self.node_details_ui.timeout_rate_label.setText(
                html_format_begin + str(self.node_details_content.timeoutRate) + html_format_end)  # get timeout rate
            self.node_details_ui.user_agent_label.setText(
                html_format_begin + str(self.node_details_content.userAgent) + html_format_end)  # get user agent
            self.node_details_ui.protocol_version_label.setText(
                html_format_begin + str(self.node_details_content.protocol) + html_format_end)  # get protocol version
            self.node_details_ui.response_time_label.setText(html_format_begin + str(
                self.node_details_content.responseTime) + html_format_end)  # get farmer node response time
            self.node_details_ui.port_label.setText(
                html_format_begin + str(self.node_details_content.port) + html_format_end)  # get farmer node port
            self.node_details_ui.node_id_label.setText(
                html_format_begin + str(self.nodeid) + html_format_end)  # get farmer node response time

            # ping_to_node = self.tools.measure_ping_latency(str(self.node_details_content.address))

            ip_addr = socket.gethostbyname(str(self.node_details_content.address))

            obj = IPWhois(ip_addr)
            res = obj.lookup_whois()
            country = res["nets"][0]['country']

            country_parsed = pycountry.countries.get(alpha_2=str(country))

            country_full_name = country_parsed.name

            self.node_details_ui.country_label.setText(
                html_format_begin + str(country_full_name) + html_format_end)  # set full country name

            ### Display country flag ###

            self.scene = QtGui.QGraphicsScene()

            # scene.setSceneRect(-600,-600, 600,600)
            # self.scene.setSceneRect(-600, -600, 1200, 1200)

            # pic = QtGui.QPixmap("PL.png")
            # self.scene.addItem(QtGui.QGraphicsPixmapItem(pic))
            # self.view = self.node_details_ui.country_graphicsView
            # self.view.setScene(self.scene)
            # self.view.setRenderHint(QtGui.QPainter.Antialiasing)
            # self.view.show()

            grview = self.node_details_ui.country_graphicsView()
            scene = QtGui.QGraphicsScene()
            scene.addPixmap(QtGui.QPixmap('PL.png'))
            grview.setScene(scene)

            grview.show()

            logger.info(country_full_name)
        except sjexc.StorjBridgeApiError as e:
            self.emit(QtCore.SIGNAL("showBridgeExceptionMessageBox"), str(e))  # emit signal to show message box with bridge exception
        except Exception as e:
            self.emit(QtCore.SIGNAL("showUnhandledExceptionMessageBox"), str(e))  # emit signal to show message box with unhandled exception

@@ -84,18 +84,19 @@
 details_
-ui
+new
  import 
@@ -1799,36 +1799,16 @@
         
- html_format_begin +
  str(sel
@@ -1839,33 +1839,16 @@
 ddress) 
-+ html_format_end
 )  # get
@@ -1942,37 +1942,16 @@
         
-  html_format_begin +
  str(sel
@@ -1986,33 +1986,16 @@
 imeout) 
-+ html_format_end
 )  # get
@@ -2084,36 +2084,16 @@
         
- html_format_begin +
  str(sel
@@ -2127,34 +2127,16 @@
 outRate)
- + html_format_end
 )  # get
@@ -2223,36 +2223,16 @@
         
- html_format_begin +
  str(sel
@@ -2265,33 +2265,16 @@
 rAgent) 
-+ html_format_end
 )  # get
@@ -2366,35 +2366,16 @@
         
-html_format_begin +
  str(sel
@@ -2407,33 +2407,16 @@
 otocol) 
-+ html_format_end
 )  # get
@@ -2494,35 +2494,16 @@
 setText(
-html_format_begin +
  str(%0A  
@@ -2555,34 +2555,16 @@
 nseTime)
- + html_format_end
 )  # get
@@ -2659,35 +2659,16 @@
         
-html_format_begin +
  str(sel
@@ -2696,33 +2696,16 @@
 t.port) 
-+ html_format_end
 )  # get
@@ -2798,62 +2798,25 @@
     
-html_format_begin + str(self.nodeid) + html_format_end
+str(self.nodeid) 
 )  #
@@ -3359,27 +3359,8 @@
     
-html_format_begin +
  str
@@ -3382,26 +3382,8 @@
 ame)
- + html_format_end
 )  #

Update node_details.py