# Path: server_client/server/models/user.py
# class User():
#     """System user"""   
#     table_name = "users"
#     column_headers_and_types = [['id', 'integer primary key'], 
#                                 ['name', 'text'], 
#                                 ['password', 'text'], 
#                                 ['logged_in', 'integer'],]
# 
#     COL_PASS  = 3
#     COL_LOGGED_IN = 4
# 
#     def __init__(self, *args):
#         self.id, self.user_name, self.password, self.loged_in = args 
# 
#     def __str__(self,):
#         return "user name: " + self.user_name + " password: " + self.password
# 
# Path: backend/sys_database/database.py
# def create_db_object():
# 
#     root = None
#     for path in sys.path:
#         if path.endswith("system"):
#             root = path
# 
#     if is_RPI:
#         db_path = "/".join([root, 'backend', 'sys_database', "sys_database.db"])
#     else:   
#         db_path = "\\".join([root, 'backend', 'sys_database', "sys_database.db"])
# 
#     return Database(db_path)


import logging
import json
import tornado.web
from ..models.user import User
from backend.sys_database.database import create_db_object
        self.logger.setLevel(logging.INFO)
        return super().__init__(application, request, **kwargs)

    def login(self, data):
        
        db_user = self.db.read(User, 'name', data['name'])
        
        if db_user:
            if db_user.loged_in:
                self.write("ok")
                self.logger.info("LOGGED IN: " + str(db_user))                 
                return True 

            elif data['password'] == db_user.password:
                db_user.loged_in = True;
                self.db.update_field(db_user, 'logged_in', 1)
                self.write("ok") 
                self.logger.info("LOGGED IN: " + str(db_user))                 
                return True
                            
            else:
                self.logger.info("WRONG PASSWORD: " + str(db_user)) 
                self.write("error");
                return False
        else:
            self.logger.info("NOT IN DATABASE: " + str(db_user))
            self.write("error")
            return False

    def logout(self, data):

# Path: server_client/server/models/user.py
# class User():
#     """System user"""   
#     table_name = "users"
#     column_headers_and_types = [['id', 'integer primary key'], 
#                                 ['name', 'text'], 
#                                 ['password', 'text'], 
#                                 ['logged_in', 'integer'],]
# 
#     COL_PASS  = 3
#     COL_LOGGED_IN = 4
# 
#     def __init__(self, *args):
#         self.id, self.user_name, self.password, self.loged_in = args 
# 
#     def __str__(self,):
#         return "user name: " + self.user_name + " password: " + self.password
# 
# Path: backend/sys_database/database.py
# def create_db_object():
# 
#     root = None
#     for path in sys.path:
#         if path.endswith("system"):
#             root = path
# 
#     if is_RPI:
#         db_path = "/".join([root, 'backend', 'sys_database', "sys_database.db"])
#     else:   
#         db_path = "\\".join([root, 'backend', 'sys_database', "sys_database.db"])
# 
#     return Database(db_path)


# Path: server_client/server/handlers/auth_handler.py
import logging
import json
import tornado.web
from ..models.user import User
from backend.sys_database.database import create_db_object

        self.logger.setLevel(logging.INFO)
        return super().__init__(application, request, **kwargs)

    def login(self, data):
        
        db_user = self.db.read(User, 'name', data['name'])
        
        if db_user:
            if db_user.loged_in:
                self.write("ok")
                self.logger.info("LOGGED IN: " + str(db_user))                 
                return True 

            elif data['password'] == db_user.password:
                db_user.loged_in = True;
                self.db.update_field(db_user, 'logged_in', 1)
                self.write("ok") 
                self.logger.info("LOGGED IN: " + str(db_user))                 
                return True
                            
            else:
                self.logger.info("WRONG PASSWORD: " + str(db_user)) 
                self.write("error");
                return False
        else:
            self.logger.info("NOT IN DATABASE: " + str(db_user))
            self.write("error")
            return False

    def logout(self, data):

        self.db = create_db_object()