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

from __future__ import division

import monetary
from django_logit import logit
from django_response import response
from paginator import pagination

from mch.models import AdministratorInfo, ModelInfo
from utils.error.errno_utils import AdministratorStatusCode, ProductModelStatusCode


@logit
def model_add(request):
    model_name = request.POST.get('model_name', '')
    model_full_name = request.POST.get('model_full_name', '')
    image_path = request.POST.get('image_path', '')
    factory_yuan = request.POST.get('factory_yuan', 1000)
    integral = int(request.POST.get('integral', 100))

    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)

    ModelInfo.objects.create(
        brand_id=administrator.brand_id,
        model_name=model_name,
        model_full_name=model_full_name,
        image=image_path,
        factory_yuan=factory_yuan,
        factory_fee=monetary.Yuan2Fen(factory_yuan),
        integral=integral,
    )

    return response(200, 'Model Add Success', u'型号添加成功')


@logit
def model_delete(request):
    model_id = request.POST.get('model_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:
        modelObj = ModelInfo.objects.get(brand_id=administrator.brand_id, model_id=model_id, status=True)
    except ModelInfo.DoesNotExist:
        return response(ProductModelStatusCode.MODEL_NOT_FOUND)

    modelObj.status = False
    modelObj.save()

    return response(200, 'Model Delete Success', u'型号删除成功')


@logit
def model_update(request):
    model_id = request.POST.get('model_id', '')
    model_name = request.POST.get('model_name', '')
    model_full_name = request.POST.get('model_full_name', '')
    image_path = request.POST.get('image_path', '')
    factory_yuan = request.POST.get('factory_yuan', 1000)
    integral = int(request.POST.get('integral', 100))

    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:
        modelObj = ModelInfo.objects.get(brand_id=administrator.brand_id, model_id=model_id, status=True)
    except ModelInfo.DoesNotExist:
        return response(ProductModelStatusCode.MODEL_NOT_FOUND)

    if model_name:
        modelObj.model_name = model_name
    if model_full_name:
        modelObj.model_full_name = model_full_name
    if image_path:
        modelObj.distributor_province_name = image_path
    if factory_yuan:
        modelObj.factory_yuan = factory_yuan
        modelObj.factory_fee = monetary.Yuan2Fen(factory_yuan)
    if integral:
        modelObj.integral = integral

    modelObj.save()

    return response(200, 'Model Update Success', u'型号更新成功')


@logit
def model_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)

    models = ModelInfo.objects.filter(brand_id=administrator.brand_id, status=True).order_by('-pk')
    count = models.count()
    models, left = pagination(models, page, num)
    models = [mdl.admindata for mdl in models]

    return response(200, 'Get Model List Success', u'获取型号列表成功', {
        'models': models,
        'count': count,
        'left': left,
    })
