# -*- coding: utf-8 -*-

from __future__ import division

from django.conf import settings
from django.contrib.auth.hashers import make_password
from django_logit import logit
from django_response import response
from paginator import pagination

from mch.models import AdministratorInfo, OperatorInfo
from utils.error.errno_utils import AdministratorStatusCode, OperatorStatusCode


@logit
def operator_add(request):
    name = request.POST.get('name', '')
    phone = request.POST.get('phone', '')
    password = request.POST.get('password', '')

    admin_id = request.session.get('admin_id')

    try:
        administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
    except AdministratorInfo.DoesNotExist:
        return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)

    try:
        operator = OperatorInfo.objects.get(brand_id=administrator.brand_id, phone=phone, user_status=OperatorInfo.ACTIVATED, status=True)
    except OperatorInfo.DoesNotExist:
        operator = None

    if operator:
        return response(OperatorStatusCode.OPERATOR_PHONE_ALREADY_EXISTS)

    encryption = make_password(password, settings.MAKE_PASSWORD_SALT, settings.MAKE_PASSWORD_HASHER)

    OperatorInfo.objects.create(
        brand_id=administrator.brand_id,
        name=name,
        phone=phone,
        encryption=encryption,
    )

    return response(200, 'Operator Add Success', u'操作员添加成功')


@logit
def operator_delete(request):
    operator_id = request.POST.get('operator_id', '')

    admin_id = request.session.get('admin_id')

    try:
        administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
    except AdministratorInfo.DoesNotExist:
        return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)

    try:
        operator = OperatorInfo.objects.get(brand_id=administrator.brand_id, operator_id=operator_id, status=True)
    except OperatorInfo.DoesNotExist:
        return response(OperatorStatusCode.OPERATOR_NOT_FOUND)

    if operator.user_status == OperatorStatusCode.OPERATOR_HAS_DISABLED:
        return response(OperatorStatusCode.OPERATOR_HAS_DISABLED)
    elif operator.user_status == OperatorStatusCode.OPERATOR_HAS_DELETED:
        return response(OperatorStatusCode.OPERATOR_HAS_DELETED)

    operator.user_status = OperatorInfo.DELETED
    operator.save()

    return response(200, 'Operator Delete Success', u'操作员删除成功')


@logit
def operator_update(request):
    operator_id = request.POST.get('operator_id', '')
    name = request.POST.get('name', '')
    password = request.POST.get('password', '')

    admin_id = request.session.get('admin_id')

    try:
        administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
    except AdministratorInfo.DoesNotExist:
        return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)

    try:
        operator = OperatorInfo.objects.get(brand_id=administrator.brand_id, operator_id=operator_id, status=True)
    except OperatorInfo.DoesNotExist:
        return response(OperatorStatusCode.OPERATOR_NOT_FOUND)

    if operator.user_status == OperatorStatusCode.OPERATOR_HAS_DISABLED:
        return response(OperatorStatusCode.OPERATOR_HAS_DISABLED)
    elif operator.user_status == OperatorStatusCode.OPERATOR_HAS_DELETED:
        return response(OperatorStatusCode.OPERATOR_HAS_DELETED)

    if name:
        operator.name = name
    if password:
        operator.encryption = make_password(password, settings.MAKE_PASSWORD_SALT, settings.MAKE_PASSWORD_HASHER)

    operator.save()

    return response(200, 'Operator Update Success', u'操作员更新成功')


@logit
def operator_list(request):
    page = request.POST.get('page', 1)
    num = request.POST.get('num', 20)

    admin_id = request.session.get('admin_id')

    try:
        administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
    except AdministratorInfo.DoesNotExist:
        return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)

    optors = OperatorInfo.objects.filter(brand_id=administrator.brand_id, user_status=OperatorInfo.ACTIVATED, status=True).order_by('-pk')
    count = optors.count()
    optors, left = pagination(optors, page, num)
    optors = [optor.data for optor in optors]

    return response(200, 'Get Operator List Success', u'获取操作员列表成功', {
        'optors': optors,
        'count': count,
        'left': left,
    })
