# Path: tsuru_autoscale/alarm/forms.py
# class AlarmForm(forms.Form):
#     name = forms.CharField()
#     expression = forms.CharField()
#     enabled = forms.BooleanField(initial=True)
#     wait = forms.IntegerField()
#     datasource = forms.ChoiceField()
#     actions = forms.MultipleChoiceField()
#     instance = forms.ChoiceField()
# 
# def datasource_list(token):
#     from tsuru_autoscale.datasource import client
#     dl = client.list(token).json() or []
#     return [(ds['Name'], ds['Name']) for ds in dl]
# 
# def action_list(token):
#     from tsuru_autoscale.action import client
#     al = client.list(token).json() or []
#     return [(ds['Name'], ds['Name']) for ds in al]
# 
# def service_instance_list(token):
#     from tsuru_autoscale.alarm import client
#     al = client.service_instance_list(token).json() or []
#     return [(ds['Name'], ds['Name']) for ds in al]
# 
# Path: tsuru_autoscale/alarm/client.py
# def host():
# def new(data, token):
# def list(token):
# def remove(name, token):
# def get(name, token):
# def service_instance_list(token):


from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from django.contrib import messages
from tsuru_autoscale.alarm.forms import AlarmForm, datasource_list, action_list, service_instance_list
from tsuru_autoscale.alarm import client



def new(request):
    token = request.GET.get("TSURU_TOKEN")

    form = AlarmForm(request.POST or None)
    form.fields['datasource'].choices = datasource_list(token)
    form.fields['actions'].choices = action_list(token)
    form.fields['instance'].choices = service_instance_list(token)

    if form.is_valid():
        client.new(form.cleaned_data, token)
        messages.success(request, u"Alarm saved.")
        url = "{}?TSURU_TOKEN={}".format(reverse("alarm-list"), token)
        return redirect(url)

    context = {"form": form}
    return render(request, "alarm/new.html", context)


def list(request):
    token = request.GET.get("TSURU_TOKEN")

# Path: tsuru_autoscale/alarm/forms.py
# class AlarmForm(forms.Form):
#     name = forms.CharField()
#     expression = forms.CharField()
#     enabled = forms.BooleanField(initial=True)
#     wait = forms.IntegerField()
#     datasource = forms.ChoiceField()
#     actions = forms.MultipleChoiceField()
#     instance = forms.ChoiceField()
# 
# def datasource_list(token):
#     from tsuru_autoscale.datasource import client
#     dl = client.list(token).json() or []
#     return [(ds['Name'], ds['Name']) for ds in dl]
# 
# def action_list(token):
#     from tsuru_autoscale.action import client
#     al = client.list(token).json() or []
#     return [(ds['Name'], ds['Name']) for ds in al]
# 
# def service_instance_list(token):
#     from tsuru_autoscale.alarm import client
#     al = client.service_instance_list(token).json() or []
#     return [(ds['Name'], ds['Name']) for ds in al]
# 
# Path: tsuru_autoscale/alarm/client.py
# def host():
# def new(data, token):
# def list(token):
# def remove(name, token):
# def get(name, token):
# def service_instance_list(token):


# Path: tsuru_autoscale/alarm/views.py
from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from django.contrib import messages
from tsuru_autoscale.alarm.forms import AlarmForm, datasource_list, action_list, service_instance_list
from tsuru_autoscale.alarm import client




def new(request):
    token = request.GET.get("TSURU_TOKEN")

    form = AlarmForm(request.POST or None)
    form.fields['datasource'].choices = datasource_list(token)
    form.fields['actions'].choices = action_list(token)
    form.fields['instance'].choices = service_instance_list(token)

    if form.is_valid():
        client.new(form.cleaned_data, token)
        messages.success(request, u"Alarm saved.")
        url = "{}?TSURU_TOKEN={}".format(reverse("alarm-list"), token)
        return redirect(url)

    context = {"form": form}
    return render(request, "alarm/new.html", context)


def list(request):
    token = request.GET.get("TSURU_TOKEN")

    alarms = client.list(token).json()