import json
from app import db

from Models.ProjectModel import ProjectModel
from Models.MediaModel import MediaModel

from Rest.Answer import Answer, MessageCodes

with open("StaticData/banned_links.json", 'r') as file:
    banned_links = [x['link'] for x in json.loads(file.read())]


class MediaREST:
    @staticmethod
    def get_media(user, media_id):
        answer = Answer()

        media = MediaModel.query.get(media_id)
        if not media or \
                user not in media.project.attendees or \
                (media.project.show_only_reviewed_media and user not in media.project.project_leader):
            return answer.add_message(MessageCodes.PROJECT_NO_SUCH_MEDIA)

        return answer.add_message(MessageCodes.WORKED).add_result(media)

    @staticmethod
    def post_media(user, json_args):
        answer = Answer()
        keys = json_args.keys()
        if "id" not in keys:
            return answer.add_message(MessageCodes.BAD_REQUEST)
        media = MediaModel.query.get(json_args["id"])
        if not media or \
                user not in media.project.attendees or \
                (media.project.show_only_reviewed_media and user not in media.project.project_leader):
            return answer.add_message(MessageCodes.PROJECT_NO_SUCH_MEDIA)
        if user not in media.project.project_leader:
            return answer.add_message(MessageCodes.PROJECT_ONLY_FOR_LEADERS)
        if "reviewed" in keys:
            media.reviewed = json_args["reviewed"]
        if "title" in keys:
            media.title = json_args["title"]
        if "description" in keys:
            media.description = json_args["description"]
        if "link" in keys:
            media.link = json_args["link"]
        if "icon" in keys:
            media.icon = json_args["icon"]
        db.session.commit()
        return answer.add_message(MessageCodes.WORKED).add_result(media)

    @staticmethod
    def delete_media(user, media_id):
        answer = Answer()

        media = MediaModel.query.get(media_id)
        if not media or \
                user not in media.project.attendees or \
                (media.project.show_only_reviewed_media and user not in media.project.project_leader):
            return answer.add_message(MessageCodes.PROJECT_NO_SUCH_MEDIA)
        if user not in media.project.project_leader:
            return answer.add_message(MessageCodes.PROJECT_ONLY_FOR_LEADERS)

        media.delete()
        return answer.add_message(MessageCodes.WORKED)

    @staticmethod
    def post_like_media(user, media_id):
        answer = Answer()

        media = MediaModel.query.get(media_id)
        if not media or \
                user not in media.project.attendees or \
                (media.project.show_only_reviewed_media and user not in media.project.project_leader):
            return answer.add_message(MessageCodes.PROJECT_NO_SUCH_MEDIA)
        if media.is_liked_by(user):
            return answer.add_message(MessageCodes.PROJECT_MEDIA_ALREADY_LIKED)
        if user is media.submitter:
            return answer.add_message(MessageCodes.PROJECT_DONT_LIKE_YOUR_OWN_MEDIA)
        media.get_liked_by(user)
        return answer.add_message(MessageCodes.WORKED).add_result(media)

    @staticmethod
    def delete_like_media(user, media_id):
        answer = Answer()

        media = MediaModel.query.get(media_id)
        if not media or \
                user not in media.project.attendees or \
                (media.project.show_only_reviewed_media and user not in media.project.project_leader):
            return answer.add_message(MessageCodes.PROJECT_NO_SUCH_MEDIA)
        if not media.is_liked_by(user):
            return answer.add_message(MessageCodes.PROJECT_MEDIA_NOT_LIKED_YET)
        media.get_unliked_by(user)
        return answer.add_message(MessageCodes.WORKED).add_result(media)

    @staticmethod
    def post_submit_media(user, project_id, json_args):
        answer = Answer()
        project = ProjectModel.query.get(project_id)
        if not project:
            return answer.add_message(MessageCodes.PROJECT_NO_SUCH_PROJECT)
        if user not in project.attendees:
            return answer.add_message(MessageCodes.PROJECT_ONLY_FOR_ATTENDEES)
        if user in project.project_leader:
            reviewed = True
        else:
            reviewed = False
        keys = json_args.keys()
        if "title" in keys:
            title = json_args["title"]
        else:
            title = None
        if "description" in keys:
            description = json_args["description"]
        else:
            description = None
        if "link" in keys:
            for link in banned_links:
                if link in json_args["link"]:
                    return answer.add_message(MessageCodes.PROJECT_MEDIA_LINK_IS_BANNED)
            link = json_args["link"]
        else:
            link = None
        if "icon" in keys:
            icon = json_args["icon"]
        else:
            icon = None
        media = MediaModel(
            project=project,
            order=len(MediaModel.query.filter(MediaModel.project == project).all()),
            reviewed=reviewed,
            title=title,
            description=description,
            link=link,
            icon=icon,
            submitter=user
        )
        db.session.add(media)
        db.session.commit()
        return answer.add_message(MessageCodes.WORKED)
