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 CostCenter
from brewman.models.validation_exception import ValidationError, TryCatchFunction


@view_config(route_name='cost_center_list', renderer='brewman:templates/angular_base.mako', permission='Authenticated')
@view_config(request_method='GET', route_name='cost_center_id', renderer='brewman:templates/angular_base.mako',
             permission='Cost Centers')
@view_config(request_method='GET', route_name='cost_center', renderer='brewman:templates/angular_base.mako',
             permission='Cost Centers')
def html(request):
    return {}


@view_config(request_method='POST', route_name='api_cost_center', renderer='json', permission='Cost Centers')
@TryCatchFunction
def save(request):
    item = CostCenter(request.json_body['Name'])
    DBSession.add(item)
    transaction.commit()
    return cost_center_info(item.id)


@view_config(request_method='POST', route_name='api_cost_center_id', renderer='json', permission='Cost Centers')
@TryCatchFunction
def update(request):
    item = CostCenter.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 cost_center_info(item.id)


@view_config(request_method='DELETE', route_name='api_cost_center_id', renderer='json', permission='Cost Centers')
def delete(request):
    item = request.matchdict.get('id', None)
    item = None if item is None else CostCenter.by_id(uuid.UUID(item))

    if item is None:
        response = Response("Cost Center 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("Cost Center deletion not implemented")
        response.status_int = 500
        return response


@view_config(request_method='GET', route_name='api_cost_center_id', renderer='json', permission='Cost Centers')
def show_id(request):
    return cost_center_info(uuid.UUID(request.matchdict.get('id', None)))


@view_config(request_method='GET', route_name='api_cost_center', renderer='json', permission='Cost Centers')
def show_blank(request):
    return cost_center_info(None)


@view_config(request_method='GET', route_name='api_cost_center', request_param='list', renderer='json',
             permission='Authenticated')
def show_list(request):
    list = CostCenter.list()
    cost_centers = []
    for item in list:
        cost_centers.append({'CostCenterID': item.id, 'Name': item.name, 'IsFixture': item.is_fixture,
                             'Url': request.route_url('cost_center_id', id=item.id)})
    return cost_centers


def cost_center_info(id):
    if id is None:
        return {}
    else:
        cost_center = CostCenter.by_id(id)
        return {'CostCenterID': cost_center.id, 'Name': cost_center.name, 'IsFixture': cost_center.is_fixture}


