from werkzeug.security import generate_password_hash, check_password_hash
from . import db, lm
from flask.ext.login import UserMixin


class User(UserMixin, db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(16), index=True, unique=True)
    password_hash = db.Column(db.String(64))

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)

    @staticmethod
    def register(username, password):
        user = User(username=username)
        user.set_password(password)
        db.session.add(user)
        db.session.commit()

    def __repr__(self):
        return '<User {0}>'.format(self.username)


@lm.user_loader
def load_user(id):
    return User.query.get(int(id))

class Settings(db.Model):
    __tablename__ = 'settings'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(16), index=True, unique=True)
    serialport = db.Column(db.String(16))

    def savedb(self, user, port):
        user = Settings.query.filter_by(username=user).first()
        if user is None:
            serialport = Settings(username=user, serialport=port)
            db.session.add(serialport)
            db.session.commit()
        user.serialport = port
        db.session.commit()

class Temperature(db.Model):
    __tablename__ = 'tempdata'
    id = db.Column(db.Integer, primary_key=True)
    temperature = db.Column(db.Float(3))
    date = db.Column(db.DateTime)
