# -*- coding: utf-8 -*-

########################################
# HXbot database module                #
# EoF 2016 EoF@itphx.ru                #
########################################

import HX
import threading
import time
import mysql.connector
from mysql.connector import errorcode


class HXdbClass():

    def __init__(self, hxbot):
        super(HXdbClass, self).__init__()

        self.hxbot = hxbot

        self.hxpower = None
        self.reflex = None
        self.joystick = None
        self.hxlcd = None
        self.hxcam = None

        self.__e = None
        self.__t = None

        self.is_running = False

        try:
            self.cnx = mysql.connector.connect(user=HX.DB_USER,
                                               password=HX.DB_PASS,
                                               host=HX.DB_HOST,
                                               database=HX.DB_NAME)
        except mysql.connector.Error as err:
            if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
                print("Something is wrong with your user name or password")
            elif err.errno == errorcode.ER_BAD_DB_ERROR:
                print("Database does not exist")
            else:
                print(err)
            self.cnx = None

            raise

    def start(self):
        self.hxpower = self.hxbot.hxpower
        self.reflex = self.hxbot.reflex
        self.joystick = self.hxbot.joystick
        self.hxlcd = self.hxbot.hxlcd
        self.hxcam = self.hxbot.hxcam

        self.__e = threading.Event()
        self.__t = threading.Thread(target=self.__process, args=(self.__e, ))

        self.__t.start()
        self.is_running = True

    def stop(self):
        self.__e.set()
        self.__t.join()
        self.is_running = False

        if self.cnx is not None:
            self.cnx.close()

        self.hxpower = None
        self.reflex = None
        self.joystick = None
        self.hxlcd = None
        self.hxcam = None

        self.__e = None
        self.__t = None

    def __process(self, stopRequest):
        cursor = self.cnx.cursor()
        pw = self.hxpower

        insert_state = ("INSERT INTO states "
                        "(vcc, vin, vdc, vbt, vba, vis, vzu, "
                        "ahx, aba, ain, adc, "
                        "vcc_ok, vin_ok, vdc_ok, vbt_ok, vba_ok, "
                        "vis_ok, vzu_ok, "
                        "ahx_ok, aba_ok, ain_ok, adc_ok, "
                        "lr_enabled, ba_enabled, ba_blocked, "
                        "vcc_low, vcc_high, vin_low, vin_high, "
                        "vdc_low, vdc_high, vbt_low, vbt_full, "
                        "vba_low, vba_full, vis_low, vis_high, "
                        "vzu_low, vzu_high, "
                        "ahx_low, ahx_high, aba_low, aba_high, "
                        "ain_low, ain_high, adc_low, adc_high, "
                        "pf_enabled, bl_enabled, bl_powered, "
                        "bl_error, ba_full, ba_charge) "
                        "VALUES ("
                        "%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
                        "%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
                        "%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
                        "%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
                        "%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
                        "%s, %s, %s)")

        while not stopRequest.is_set():
            # Get Data
            state = (pw.vcc, pw.vin, pw.vdc, pw.vbt, pw.vba, pw.vis,
                     pw.vzu, pw.ahx, pw.aba, pw.ain, pw.adc, pw.vcc_ok,
                     pw.vin_ok, pw.vdc_ok, pw.vbt_ok, pw.vba_ok,
                     pw.vis_ok, pw.vzu_ok,
                     pw.ahx_ok, pw.aba_ok, pw.ain_ok, pw.adc_ok,
                     pw.lr_enabled, pw.ba_enabled, pw.ba_blocked,
                     pw.vcc_low, pw.vcc_high, pw.vin_low, pw.vin_high,
                     pw.vdc_low, pw.vdc_high, pw.vbt_low, pw.vbt_full,
                     pw.vba_low, pw.vba_full, pw.vis_low, pw.vis_high,
                     pw.vzu_low, pw.vzu_high, pw.ahx_low, pw.ahx_high,
                     pw.aba_low, pw.aba_high, pw.ain_low, pw.ain_high,
                     pw.adc_low, pw.adc_high,
                     pw.pf_enabled, pw.bl_enabled,
                     pw.bl_powered, pw.bl_error,
                     pw.ba_full, pw.ba_charge)

            # Insert Data
            if pw.online:
                cursor.execute(insert_state, state)
                self.cnx.commit()

            # Delay
            time.sleep(HX.DB_DELAY)

        cursor.close()
