# Path: recruit_app/blacklist/forms.py
# class SearchForm(Form):
#     search = StringField('Search')
#     submit = SubmitField(label='Submit')
# 
# class BlacklistCharacterForm(Form):
#     character_name = TextAreaField("Character Name", validators=[Length(min=-1, max=2000, message='Max length %(max)d')])
#     main_name      = TextAreaField("Main Name",      validators=[Length(min=-1, max=2000, message='Max length %(max)d')])
#     corporation    = TextAreaField("Corporation",    validators=[Length(min=-1, max=2000, message='Max length %(max)d')])
#     alliance       = TextAreaField("Alliance",       validators=[Length(min=-1, max=2000, message='Max length %(max)d')])
#     ip_address     = TextAreaField("IP Address",     validators=[IPAddress(), Optional()])
#     notes          = TextAreaField("Notes",          validators=[Length(min=-1, max=2000, message='Max length %(max)d')])
# 
#     submit = SubmitField(label='Submit Entry')
# 
# Path: recruit_app/blacklist/models.py
# class BlacklistCharacter(SurrogatePK, TimeMixin, Model):
#     __tablename__ = 'blacklist_character'
#     # __searchable__ = ['name', 'main_name', 'corporation', 'alliance', 'notes', 'ip_address']
# 
#     name = Column(db.Unicode, nullable=True)
#     main_name = Column(db.Unicode, nullable=True)
#     corporation = Column(db.Unicode)
#     alliance = Column(db.Unicode)
#     notes = Column(db.Unicode)
# 
#     ip_address = Column(db.Unicode)
# 
#     creator_id = ReferenceCol('users', nullable=True)
#     creator = relationship('User', foreign_keys=[creator_id], backref='blacklist_character_entries')
# 
#     def __repr__(self):
#         return '<' + self.name + ': ' + self.notes + '>'
# 
# Path: recruit_app/blacklist/managers.py
# class BlacklistManager:
#     def __init__(self):
#         pass
# 
#     @staticmethod
#     def create_entry(entry_data, user):
#         try:
#             entry = BlacklistCharacter()
#             entry.name = entry_data.character_name.data
#             entry.main_name = entry_data.main_name.data
#             entry.corporation = entry_data.corporation.data
#             entry.alliance = entry_data.alliance.data
#             entry.ip_address = entry_data.ip_address.data
#             entry.notes = entry_data.notes.data
#             entry.creator = user
#             entry.save()
# 
#             return True
#         except:
#             return False


from flask import Blueprint, render_template, request, redirect, url_for, flash, current_app
from flask_security.decorators import login_required
from flask_security import current_user, roles_accepted
from .forms import SearchForm, BlacklistCharacterForm
from .models import BlacklistCharacter
from .managers import BlacklistManager



blueprint = Blueprint("blacklist", __name__, url_prefix='/blacklist',
                        static_folder="../static")

@blueprint.route("/", methods=['GET', 'POST'])
@blueprint.route("/<int:page>/", methods=['GET', 'POST'])
@login_required
@roles_accepted('admin', 'recruiter', 'reviewer', 'compliance', 'blacklist')
def blacklist_view(page=1):
    search_form = SearchForm()
    blacklist_form = BlacklistCharacterForm()

    blacklist = None
    
    if request.method == 'POST':
        if request.form['submit'] == 'Submit Entry':
            if current_user.has_role('admin') or current_user.has_role('compliance') or current_user.has_role('blacklist'):
                if blacklist_form.validate_on_submit():
                    if BlacklistManager.create_entry(blacklist_form, current_user):
                        flash('Entry Added')
            else:
                flash("You don't have the proper permissions.")
        elif search_form.validate_on_submit() and search_form.search.data:
            blacklist = BlacklistCharacter.query.filter(
                BlacklistCharacter.name.ilike       ("%" + search_form.search.data + "%")|
                BlacklistCharacter.main_name.ilike  ("%" + search_form.search.data + "%")|

# Path: recruit_app/blacklist/forms.py
# class SearchForm(Form):
#     search = StringField('Search')
#     submit = SubmitField(label='Submit')
# 
# class BlacklistCharacterForm(Form):
#     character_name = TextAreaField("Character Name", validators=[Length(min=-1, max=2000, message='Max length %(max)d')])
#     main_name      = TextAreaField("Main Name",      validators=[Length(min=-1, max=2000, message='Max length %(max)d')])
#     corporation    = TextAreaField("Corporation",    validators=[Length(min=-1, max=2000, message='Max length %(max)d')])
#     alliance       = TextAreaField("Alliance",       validators=[Length(min=-1, max=2000, message='Max length %(max)d')])
#     ip_address     = TextAreaField("IP Address",     validators=[IPAddress(), Optional()])
#     notes          = TextAreaField("Notes",          validators=[Length(min=-1, max=2000, message='Max length %(max)d')])
# 
#     submit = SubmitField(label='Submit Entry')
# 
# Path: recruit_app/blacklist/models.py
# class BlacklistCharacter(SurrogatePK, TimeMixin, Model):
#     __tablename__ = 'blacklist_character'
#     # __searchable__ = ['name', 'main_name', 'corporation', 'alliance', 'notes', 'ip_address']
# 
#     name = Column(db.Unicode, nullable=True)
#     main_name = Column(db.Unicode, nullable=True)
#     corporation = Column(db.Unicode)
#     alliance = Column(db.Unicode)
#     notes = Column(db.Unicode)
# 
#     ip_address = Column(db.Unicode)
# 
#     creator_id = ReferenceCol('users', nullable=True)
#     creator = relationship('User', foreign_keys=[creator_id], backref='blacklist_character_entries')
# 
#     def __repr__(self):
#         return '<' + self.name + ': ' + self.notes + '>'
# 
# Path: recruit_app/blacklist/managers.py
# class BlacklistManager:
#     def __init__(self):
#         pass
# 
#     @staticmethod
#     def create_entry(entry_data, user):
#         try:
#             entry = BlacklistCharacter()
#             entry.name = entry_data.character_name.data
#             entry.main_name = entry_data.main_name.data
#             entry.corporation = entry_data.corporation.data
#             entry.alliance = entry_data.alliance.data
#             entry.ip_address = entry_data.ip_address.data
#             entry.notes = entry_data.notes.data
#             entry.creator = user
#             entry.save()
# 
#             return True
#         except:
#             return False


# Path: recruit_app/blacklist/views.py
from flask import Blueprint, render_template, request, redirect, url_for, flash, current_app
from flask_security.decorators import login_required
from flask_security import current_user, roles_accepted
from .forms import SearchForm, BlacklistCharacterForm
from .models import BlacklistCharacter
from .managers import BlacklistManager




blueprint = Blueprint("blacklist", __name__, url_prefix='/blacklist',
                        static_folder="../static")

@blueprint.route("/", methods=['GET', 'POST'])
@blueprint.route("/<int:page>/", methods=['GET', 'POST'])
@login_required
@roles_accepted('admin', 'recruiter', 'reviewer', 'compliance', 'blacklist')
def blacklist_view(page=1):
    search_form = SearchForm()
    blacklist_form = BlacklistCharacterForm()

    blacklist = None
    
    if request.method == 'POST':
        if request.form['submit'] == 'Submit Entry':
            if current_user.has_role('admin') or current_user.has_role('compliance') or current_user.has_role('blacklist'):
                if blacklist_form.validate_on_submit():
                    if BlacklistManager.create_entry(blacklist_form, current_user):
                        flash('Entry Added')
            else:
                flash("You don't have the proper permissions.")
        elif search_form.validate_on_submit() and search_form.search.data:
            blacklist = BlacklistCharacter.query.filter(
                BlacklistCharacter.name.ilike       ("%" + search_form.search.data + "%")|
                BlacklistCharacter.main_name.ilike  ("%" + search_form.search.data + "%")|

                BlacklistCharacter.corporation.ilike("%" + search_form.search.data + "%")|