# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.conf.urls import url
from tastypie.resources import Resource
from tastypie.utils import trailing_slash
from api.utils.jwt_authentication import JWTAuthentication
from api.utils.docker_api import (
    get_all_images, 
    get_all_containers, 
    start_container, 
    restart_container,
    stop_container
)

'''
'''
class DockerResource(Resource):
    class Meta:
        authentication = JWTAuthentication()

    '''
    '''
    def prepend_urls(self):
        return [
            url(r'^(?P<resource_name>%s)/image/all%s$' % (self._meta.resource_name, trailing_slash), self.wrap_view('get_all_images'), name='api_get_all_images'),
            url(r'^(?P<resource_name>%s)/container/all%s$' % (self._meta.resource_name, trailing_slash), self.wrap_view('get_all_containers'), name='api_get_all_containers'),
            url(r'^(?P<resource_name>%s)/container/start/(?P<container_id>[\w\d_.-]+)%s$' % (self._meta.resource_name, trailing_slash), self.wrap_view('start_container'), name='api_start_container'),
            url(r'^(?P<resource_name>%s)/container/restart/(?P<container_id>[\w\d_.-]+)%s$' % (self._meta.resource_name, trailing_slash), self.wrap_view('restart_container'), name='api_restart_container'),
            url(r'^(?P<resource_name>%s)/container/stop/(?P<container_id>[\w\d_.-]+)%s$' % (self._meta.resource_name, trailing_slash), self.wrap_view('stop_container'), name='api_stop_container'),
        ]

    '''
    '''
    def get_all_images(self, request, **kwargs):
        self.method_check(request, allowed='get')
        self.is_authenticated(request)

        bundle = self.build_bundle(obj={
            'images': get_all_images()
        }, request=request)

        return self.create_response(request, bundle.obj)

    '''
    '''
    def get_all_containers(self, request, **kwargs):
        self.method_check(request, allowed='get')
        self.is_authenticated(request)

        return self.create_response(request, {
            'containers': get_all_containers()
        })

    '''
    '''
    def start_container(self, request, **kwargs):
        self.method_check(request, allowed='post')
        self.is_authenticated(request)

        container_id = kwargs.get('container_id', None)

        if not container_id:
            return self.create_response(request, {
                'error_message': 'container id is required'
            }, request=request)

        container_data = start_container(container_id)

        if not container_data:
            return self.create_response(request, {
                'error_message': 'cannot start container %s' % container_id
            }, request=request)


        return self.create_response(request, {
            'container': container_data
        })

    '''
    '''
    def restart_container(self, request, **kwargs):
        self.method_check(request, allowed='post')
        self.is_authenticated(request)

        container_id = kwargs.get('container_id', None)

        if not container_id:
            return self.create_response({
                'error_message': 'container id is required'
            }, request=request)

        container_data = restart_container(container_id)

        if not container_data:
            return self.create_response({
                'error_message': 'cannot restart container %s' % container_id
            })

        return self.create_response(request, {
            'container': container_data
        })

    '''
    '''
    def stop_container(self, request, **kwargs):
        self.method_check(request, allowed='post')
        self.is_authenticated(request)

        container_id = kwargs.get('container_id', None)

        if not container_id:
            return self.create_response({
                'error_message': 'container id is required'
            })

        container_data = stop_container(container_id)

        if not container_data:
            return self.create_response({
                'error_message': 'cannot stop container %s' % container_id
            })
            
        return self.create_response(request, {
            'container': container_data
        })