from PyQt5 import QtWidgets, QtCore
from pkg_resources import parse_version
import sys
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
import requests
from requests.exceptions import HTTPError
import json
import os
import re

def initDisplay(self):
    #Set initial display background color
    self.textBrowser.setStyleSheet("background-color: white; padding: 50px;")
    self.textBrowser.setAlignment(QtCore.Qt.AlignCenter)
    self.textBrowser.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByMouse)
    versionText = getVersionText(self)
    #self.displayView.setBackgroundBrush(brush)
    txt = '\
    <div style="color: #202020;">\
        <h2 style="text-align: center">\
            Welcome to Electrochemical Parser v%s!\
        </h2>\
        <span style="text-align: center;font-size:10pt;">\
            <br>\
            Author: Vincent Wu (vincentwu@ucsb.edu)\
            <br>\
        </span>\
        <p style="text-align:left;font-size:10pt;">\
            This program tool allows researchers to quickly extract elecrochemical data and plot charge/\
            discharge curves from raw excel data files generated by the Arbin MITS Pro electrochemical cycler. \
            It is meant to speed up the data collection and analysis process for battery scientists. The tool \
            is easy to use and will generate both a customizable charge/ discharge plot and a summary table \
            containing information such as average voltage, capacities, Coulombic efficiencies, and more. \
            For more detailed instructions and to access the open-source code,\
            please refer to the project'"'"'s github page: \
            <a href="https://github.com/vince-wu/electrochemical-parsing">\
            https://github.com/vince-wu/electrochemical-parsing </a>.\
        </p>\
            %s\
    </div>'%(self.version, versionText)
    text = self.textBrowser.setText(txt)
    self.textBrowser.setOpenExternalLinks(True)

def getVersionText(self):
    color = '#000'
    boldcolor = '#00f'
    versionText = ''
    debug = False
    connected = True
    if debug:
        newestVersion = 'v2.0.0'
        bodyText = ''
    if not debug:
        #Get program's newest version from Github API, compare to current version
        json_response = getJson()
        # json_response = None
        if json_response:
            newestVersion = json_response['tag_name']
            bodyText = json_response['body']
            bodyText = re.sub(r'(<li>)', '<li> ', bodyText)
            bodyText = re.sub(r'(Changelog:)', '{} Changelog:'.format(newestVersion), bodyText)
            newestVersionNumber = re.sub('[^0-9,.]','',newestVersion)
        else:
            connected = False

    currentVersionNumber = re.sub('[^0-9,.]','',self.version)
    #Alert user based on if version is updated, ahead, or behind
    if connected == False:
        versionText = '\
        <p style="color: %s; font-size:10pt; text-align:center">\
            <br>\
            <span style="color: %s">\
                ***\
            </span>\
            To see if this program is up to date, please connect to the internet or \
            visit \
            <a style="color: " href="https://github.com/vince-wu/electrochemical-parsing/releases/latest">\
                this link\
            </a>\
            .\
            <span style="color: %s">\
                ***\
            </span>\
        </p>\
        '%(color, boldcolor, boldcolor)
    elif parse_version(newestVersionNumber) > parse_version(currentVersionNumber):
        versionText = '\
        <p style="color: %s; font-size:10pt; text-align:center">\
            <br>\
            <span style="color: %s">\
                ***\
            </span>\
            An updated version of this software \
            is available\
            <a style="color:" href="https://github.com/vince-wu/electrochemical-parsing/releases/latest">\
                here\
            </a>\
            !\
            <span style="color: %s">\
                ***\
            </span>\
        </p>\
        <p style="color: %s; font-size:9pt">\
            %s\
        </p>\
        '%(color, boldcolor, boldcolor, color, bodyText)

    elif parse_version(newestVersionNumber) == parse_version(currentVersionNumber):
        versionText = '\
        <p style="color: %s; font-size:10pt; text-align:center">\
            <br>\
            <span style="color: %s">\
                ***\
            </span>\
                This program version is up to date!\
            <span style="color: %s">\
                ***\
            </span>\
            <p style="color: %s; font-size:9pt">\
                %s\
            </p>\
        </p>\
        '%(color, boldcolor, boldcolor, color, bodyText)
    elif parse_version(newestVersionNumber) < parse_version(currentVersionNumber):
        versionText = '\
        <p style="color: %s; font-size:10pt; text-align:center">\
            <br>\
            <span style="color: %s">\
                ***\
            </span>\
                You are running an unpublished pre-release version of the program!  \
                Please report any bugs that you find to vincentwu@ucsb.edu.\
            <span style="color: %s">\
                ***\
            </span>\
            <p style="color: %s; font-size:9pt">\
                %s\
            </p>\
        </p>\
        '%(color, boldcolor, boldcolor, color, bodyText)
    return versionText

def getJson():
	try:
		response = requests.get(
			'https://api.github.com/repos/vince-wu/electrochemical-parsing/releases/latest'
			)
		json_response = response.json()
		return json_response
	except HTTPError as http_err:
		return None
	except Exception as err:
		return None


def readInputFromSave(self):
    if os.path.isfile('savefile.txt'):
        f = open('savefile.txt', 'r')
        lines = f.readlines()
        for l in lines:
            input = [x.strip() for x in l.split(',')]
            field = input[0]
            val = input[1]
            if field == 'initCycle':
                self.first_cycle_spinBox.setValue(int(val))
            elif field == 'numCycles':
                self.num_cycles_spinBox.setValue(int(val))
            elif field == 'mass':
                self.mass_doubleSpinBox.setValue(float(val))
            elif field == 'ratio':
                self.ratio_input.setText(val)
            elif field == 'rate':
                self.rate_comboBox.setCurrentText(val)
            elif field == 'type':
                self.type_input.setText(val)
            elif field == 'anode':
                self.anode_input.setText(val)
            elif field == 'dataPath':
                self.raw_inputfile.setText(val)
            elif field == 'figurePath':
                self.figure_path.setText(val)
            elif field == 'tablePath':
                self.table_path.setText(val)

class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.fig.add_subplot(111)
        self.gca = self.fig.gca()
        super(MplCanvas, self).__init__(self.fig)

def setUpGraph(self):
    dpi = 100
    height = self.textBrowser.frameGeometry().height()/dpi
    width = self.textBrowser.frameGeometry().width()/dpi
    self.graphWindow = MplCanvas(self, width, height, dpi)
    self.verticalLayout.insertWidget(0, self.graphWindow)
    self.verticalLayout.removeWidget(self.textBrowser)
    self.textBrowser.deleteLater()
    self.textBrowser = None
    self.display_exists = False

def displayError(self, text):
    msg = QtWidgets.QMessageBox()
    msg.setIcon(QtWidgets.QMessageBox.Warning)
    msg.setWindowTitle("Error")
    msg.setText(text)
    msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
    msg.exec_()