# -*- 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):
    jancode = request.POST.get('jancode', '')
    model_name = request.POST.get('model_name', '')
    model_uni_name = request.POST.get('model_uni_name', '')
    model_full_name = request.POST.get('model_full_name', '')
    image_path = request.POST.get('image_path', '')
    image2_path = request.POST.get('image2_path', '')
    factory_yuan = request.POST.get('factory_yuan', 1000)
    integral = int(request.POST.get('integral', 100))

    category = request.POST.get('category', '')
    warehouse = request.POST.get('warehouse', '')

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

    is_important = request.POST.get('is_important', 0)

    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,
        jancode=jancode,
        model_name=model_name,
        model_uni_name=model_uni_name,
        model_full_name=model_full_name,
        category=category,
        warehouse=warehouse,
        image=image_path,
        image2=image2_path,
        factory_yuan=factory_yuan,
        factory_fee=monetary.Yuan2Fen(factory_yuan),
        integral=integral,
        is_important=is_important,
    )

    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):
    jancode = request.POST.get('jancode', '')
    model_id = request.POST.get('model_id', '')
    model_name = request.POST.get('model_name', '')
    model_uni_name = request.POST.get('model_uni_name', '')
    model_full_name = request.POST.get('model_full_name', '')
    image_path = request.POST.get('image_path', '')
    image2_path = request.POST.get('image2_path', '')
    factory_yuan = request.POST.get('factory_yuan', 1000)
    integral = int(request.POST.get('integral', 100))

    category = request.POST.get('category', '')
    warehouse = request.POST.get('warehouse', '')

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

    is_important = request.POST.get('is_important', 0)

    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 jancode:
        modelObj.jancode = jancode
    if model_name:
        modelObj.model_name = model_name
    if model_uni_name:
        modelObj.model_uni_name = model_uni_name
    if model_full_name:
        modelObj.model_full_name = model_full_name
    if category:
        modelObj.category = category
    if warehouse:
        modelObj.warehouse = warehouse
    if image_path:
        modelObj.image = image_path
    if image2_path:
        modelObj.image2 = image2_path
    if factory_yuan:
        modelObj.factory_yuan = factory_yuan
        modelObj.factory_fee = monetary.Yuan2Fen(factory_yuan)
    if integral:
        modelObj.integral = integral

    modelObj.is_important = is_important

    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, display=True, 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,
    })
