import uuid
from pyramid.response import Response

from pyramid.view import view_config
import transaction
from brewman.models import DBSession

from brewman.models.master import ProductGroup
from brewman.models.validation_exception import ValidationError, TryCatchFunction


@view_config(route_name='product_group_list', renderer='brewman:templates/angular_base.mako',
             permission='Authenticated')
@view_config(request_method='GET', route_name='product_group_id', renderer='brewman:templates/angular_base.mako',
             permission='Product Groups')
@view_config(request_method='GET', route_name='product_group', renderer='brewman:templates/angular_base.mako',
             permission='Product Groups')
def html(request):
    return {}


@view_config(request_method='POST', route_name='api_product_group', renderer='json', permission='Product Groups')
@TryCatchFunction
def save(request):
    item = ProductGroup(request.json_body['Name'])
    DBSession.add(item)
    transaction.commit()
    return product_group_info(item.id)


@view_config(request_method='POST', route_name='api_product_group_id', renderer='json', permission='Product Groups')
@TryCatchFunction
def update(request):
    item = ProductGroup.by_id(uuid.UUID(request.matchdict['id']))
    if item.is_fixture:
        raise ValidationError("{0} is a fixture and cannot be edited or deleted.".format(item.name))
    item.name = request.json_body['Name']
    transaction.commit()
    return product_group_info(item.id)


@view_config(request_method='DELETE', route_name='api_product_group_id', renderer='json', permission='Product Groups')
def delete(request):
    item = request.matchdict.get('id', None)
    item = None if item is None else ProductGroup.by_id(uuid.UUID(item))

    if item is None:
        response = Response("Product Group not Found")
        response.status_int = 500
        return response
    elif item.is_fixture:
        raise ValidationError("{0} is a fixture and cannot be edited or deleted.".format(item.name))
    else:
        response = Response("Product Group deletion not implemented")
        response.status_int = 500
        return response


@view_config(request_method='GET', route_name='api_product_group_id', renderer='json', permission='Product Groups')
def show_id(request):
    return product_group_info(uuid.UUID(request.matchdict.get('id', None)))


@view_config(request_method='GET', route_name='api_product_group', renderer='json', permission='Product Groups')
def show_blank(request):
    return product_group_info(None)


@view_config(request_method='GET', route_name='api_product_group', request_param='list', renderer='json',
             permission='Authenticated')
def show_list(request):
    list = ProductGroup.list()
    product_groups = []
    for item in list:
        product_groups.append({'ProductGroupID': item.id, 'Name': item.name, 'IsFixture': item.is_fixture,
                               'Url': request.route_url('product_group_id', id=item.id)})
    return product_groups


def product_group_info(id):
    if id is None:
        return {}
    else:
        product_group = ProductGroup.by_id(id)
        return {'ProductGroupID': product_group.id, 'Name': product_group.name, 'IsFixture': product_group.is_fixture}


