from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from fileserver.HashCalc import HashSumType, hashsum, hashsum_of_file
from fileserver.ResponseWorker import ResponseByType, ResponseType

from fileserver.AdminWorker import InitializeVariables, GenerateAdmin, \
    CheckAdminFromPostRequest, ChangeAdminPasswordFromPostRequest, \
    CreateUserFromPostRequest, ResetUserPasswordFromPostRequest, \
    DeleteUserFromPostRequest, GetUsersList, GetFileList, \
    AdminGetFileResponseFromPostRequest, AdminDeleteFileFromPostRequest, \
    CleanMediaDirectory

from fileserver.OptionWorker import IsInitialized, GetOptionList, \
    ChangeOptionFromPostRequest

from fileserver.ResponseFormatWorker import GenerateOutput
from fileserver.OutputTableHeader import OutputTableHeader

@csrf_exempt
def initialize_server(request):
    try:
        if IsInitialized():
            return ResponseByType(ResponseType.PermissionDenied, request)

        initializeVariablesCode = InitializeVariables()
        if (initializeVariablesCode != ResponseType.OK):
            return ResponseByType(initializeVariablesCode, request)

        (generateAdminCode, (login, password)) = GenerateAdmin()
        if (generateAdminCode != ResponseType.OK):
            return ResponseByType(generateAdminCode, request)

        header = OutputTableHeader.InitializeServer.value
        data = ((login, password),)
        outputTable = GenerateOutput(header, data, request)

        return HttpResponse(outputTable)
    except Exception:
        return ResponseByType(ResponseType.UnknownError, request)

@csrf_exempt
def change_admin_password(request):
    try:
        checkAdminCode = CheckAdminFromPostRequest(request)
        if (checkAdminCode != ResponseType.OK):
            return ResponseByType(checkAdminCode, request)

        changeAdminPasswordCode = ChangeAdminPasswordFromPostRequest(request)
        if (changeAdminPasswordCode != ResponseType.OK):
            return ResponseByType(changeAdminPasswordCode, request)

        return ResponseByType(ResponseType.OK, request)
    except Exception:
        return ResponseByType(ResponseType.UnknownError, request)

@csrf_exempt
def register_user(request):
    try:
        checkAdminCode = CheckAdminFromPostRequest(request)
        if (checkAdminCode != ResponseType.OK):
            return ResponseByType(checkAdminCode, request)

        (createUserCode, password) = CreateUserFromPostRequest(request)
        if (createUserCode != ResponseType.OK):
            return ResponseByType(createUserCode, request)

        header = OutputTableHeader.RegisterUser.value
        data = ((password,),)
        outputTable = GenerateOutput(header, data, request)

        return HttpResponse(outputTable)
    except Exception:
        return ResponseByType(ResponseType.UnknownError, request)

@csrf_exempt
def reset_user_password(request):
    try:
        checkAdminCode = CheckAdminFromPostRequest(request)
        if (checkAdminCode != ResponseType.OK):
            return ResponseByType(checkAdminCode, request)

        (resetUserPasswordCode, password) = \
            ResetUserPasswordFromPostRequest(request)
        if (resetUserPasswordCode != ResponseType.OK):
            return ResponseByType(resetUserPasswordCode, request)

        header = OutputTableHeader.ResetUserPassword.value