# Path: hashview/task_groups/forms.py
# class TaskGroupsForm(FlaskForm):
#     name = StringField('Name', validators=([DataRequired()]))
#     submit = SubmitField('Create')  
# 
#     def validate_task(self, name):
#         task = Tasks.query.filter_by(name = name.data).first()
#         if task:
#             raise ValidationError('That task name is taken. Please choose a different one.')
# 
# Path: hashview/models.py
# class Tasks(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(100), nullable=False)
#     hc_attackmode = db.Column(db.String(25), nullable=False) # dictionary, mask, bruteforce, combinator
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
#     wl_id = db.Column(db.Integer)
#     rule_id = db.Column(db.Integer)
#     hc_mask = db.Column(db.String(50))
# 
# class TaskGroups(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50), nullable=False)
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
#     tasks = db.Column(db.String(256), nullable=False)
# 
# class Users(db.Model, UserMixin):
#     id = db.Column(db.Integer, primary_key=True)
#     first_name = db.Column(db.String(20), nullable=False)
#     last_name = db.Column(db.String(20), nullable=False)
#     email_address = db.Column(db.String(50), unique=True, nullable=False)
#     password = db.Column(db.String(60), nullable=False)
#     admin = db.Column(db.Boolean, nullable=False, default=False)
#     pushover_app_id = db.Column(db.String(50), nullable=True)
#     pushover_user_key = db.Column(db.String(50), nullable=True)
#     wordlists = db.relationship('Wordlists', backref='tbd', lazy=True)
#     rules = db.relationship('Rules', backref='owner', lazy=True)
#     jobs = db.relationship('Jobs', backref='owner', lazy=True)
#     tasks = db.relationship('Tasks', backref='owner', lazy=True)
#     taskgroups = db.relationship('TaskGroups', backref='owner', lazy=True)
# 
#     def get_reset_token(self, expires_sec=1800):
#         s = Serializer(Config.SECRET_KEY, expires_sec)
#         return s.dumps({'user_id': self.id}).decode('utf-8')
# 
#     @staticmethod
#     def verify_reset_token(token):
#         s = Serializer(Config.SECRET_KEY)
#         try:
#             user_id = s.loads(token)['user_id']
#         except:
#             return None     
#         return Users.query.get(user_id)
# 
# Path: hashview.py
# def data_retention_cleanup():


from flask import Blueprint, render_template, redirect, url_for, flash, abort
from flask_login import login_required, current_user
from hashview.task_groups.forms import TaskGroupsForm
from hashview.models import Tasks, TaskGroups, Users
from hashview import db
import json
@task_groups.route("/task_groups/assigned_tasks/<int:task_group_id>/demote_task/<int:task_id>", methods=['GET'])
@login_required
def task_groups_assigned_tasks_demote_task(task_group_id, task_id):
    task_group = TaskGroups.query.get(task_group_id)
    task_group_tasks = json.loads(task_group.tasks)
    if task_group_tasks[-1] == task_id:
        # Cant demote further
        return redirect("/task_groups/assigned_tasks/"+str(task_group.id))
    else:
        new_task_group_tasks = []
        # Creating manual index since for loop doesnt allow you to modify the itterator value
        index = 0
        while index < len(task_group_tasks):
            if index+1 < len(task_group_tasks):
                if task_group_tasks[index] == task_id:
                    new_task_group_tasks.append(task_group_tasks[index+1])
                    new_task_group_tasks.append(task_id)
                    index = index + 1
                else:
                    new_task_group_tasks.append(task_group_tasks[index])
            else:
                new_task_group_tasks.append(task_group_tasks[index])
            index+=1
    task_group.tasks = str(new_task_group_tasks)
    db.session.commit()
    return redirect("/task_groups/assigned_tasks/"+str(task_group.id))

@task_groups.route("/task_groups/delete/<int:task_group_id>", methods=['POST'])
@login_required
def task_groups_delete(task_group_id):

# Path: hashview/task_groups/forms.py
# class TaskGroupsForm(FlaskForm):
#     name = StringField('Name', validators=([DataRequired()]))
#     submit = SubmitField('Create')  
# 
#     def validate_task(self, name):
#         task = Tasks.query.filter_by(name = name.data).first()
#         if task:
#             raise ValidationError('That task name is taken. Please choose a different one.')
# 
# Path: hashview/models.py
# class Tasks(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(100), nullable=False)
#     hc_attackmode = db.Column(db.String(25), nullable=False) # dictionary, mask, bruteforce, combinator
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
#     wl_id = db.Column(db.Integer)
#     rule_id = db.Column(db.Integer)
#     hc_mask = db.Column(db.String(50))
# 
# class TaskGroups(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50), nullable=False)
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
#     tasks = db.Column(db.String(256), nullable=False)
# 
# class Users(db.Model, UserMixin):
#     id = db.Column(db.Integer, primary_key=True)
#     first_name = db.Column(db.String(20), nullable=False)
#     last_name = db.Column(db.String(20), nullable=False)
#     email_address = db.Column(db.String(50), unique=True, nullable=False)
#     password = db.Column(db.String(60), nullable=False)
#     admin = db.Column(db.Boolean, nullable=False, default=False)
#     pushover_app_id = db.Column(db.String(50), nullable=True)
#     pushover_user_key = db.Column(db.String(50), nullable=True)
#     wordlists = db.relationship('Wordlists', backref='tbd', lazy=True)
#     rules = db.relationship('Rules', backref='owner', lazy=True)
#     jobs = db.relationship('Jobs', backref='owner', lazy=True)
#     tasks = db.relationship('Tasks', backref='owner', lazy=True)
#     taskgroups = db.relationship('TaskGroups', backref='owner', lazy=True)
# 
#     def get_reset_token(self, expires_sec=1800):
#         s = Serializer(Config.SECRET_KEY, expires_sec)
#         return s.dumps({'user_id': self.id}).decode('utf-8')
# 
#     @staticmethod
#     def verify_reset_token(token):
#         s = Serializer(Config.SECRET_KEY)
#         try:
#             user_id = s.loads(token)['user_id']
#         except:
#             return None     
#         return Users.query.get(user_id)
# 
# Path: hashview.py
# def data_retention_cleanup():


# Path: hashview/task_groups/routes.py
from flask import Blueprint, render_template, redirect, url_for, flash, abort
from flask_login import login_required, current_user
from hashview.task_groups.forms import TaskGroupsForm
from hashview.models import Tasks, TaskGroups, Users
from hashview import db
import json

@task_groups.route("/task_groups/assigned_tasks/<int:task_group_id>/demote_task/<int:task_id>", methods=['GET'])
@login_required
def task_groups_assigned_tasks_demote_task(task_group_id, task_id):
    task_group = TaskGroups.query.get(task_group_id)
    task_group_tasks = json.loads(task_group.tasks)
    if task_group_tasks[-1] == task_id:
        # Cant demote further
        return redirect("/task_groups/assigned_tasks/"+str(task_group.id))
    else:
        new_task_group_tasks = []
        # Creating manual index since for loop doesnt allow you to modify the itterator value
        index = 0
        while index < len(task_group_tasks):
            if index+1 < len(task_group_tasks):
                if task_group_tasks[index] == task_id:
                    new_task_group_tasks.append(task_group_tasks[index+1])
                    new_task_group_tasks.append(task_id)
                    index = index + 1
                else:
                    new_task_group_tasks.append(task_group_tasks[index])
            else:
                new_task_group_tasks.append(task_group_tasks[index])
            index+=1
    task_group.tasks = str(new_task_group_tasks)
    db.session.commit()
    return redirect("/task_groups/assigned_tasks/"+str(task_group.id))

@task_groups.route("/task_groups/delete/<int:task_group_id>", methods=['POST'])
@login_required
def task_groups_delete(task_group_id):

    task_group = TaskGroups.query.get(task_group_id)