# Path: tsuru_autoscale/wizard/forms.py
# OPERATOR_CHOICES = (
#     ('>', '>'),
#     ('>=', '>='),
#     ('<=', '<='),
#     ('<', '<'),
#     ('!=', '!='),
# )
# AGGREGATOR_CHOICES = (
#     ('avg', 'average'),
#     ('max', 'maximum'),
# )
# class ScaleForm(forms.Form):
# class ConfigForm(forms.Form):
#     def __init__(self, min_wait, *args, **kwargs):
#     def clean_value(self):
#     def clean_step(self):
# 
# Path: tsuru_autoscale/wizard/client.py
# def host():
# def tsuru_host():
# def clean_token(token):
# def app_info(name, token):
# def pool_info(name, token):
# def process_list(instance_name, token):
# def new(data, token):
# def get(name, token):
# def remove(name, token):
# def enable(name, token):
# def disable(name, token):
# def events(name, token):
# 
# Path: tsuru_autoscale/datasource/client.py
# def host():
# def new(data, token):
# def list(token):
# def remove(name, token):
# def get(name, token):


from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from django.contrib import messages
from tsuru_autoscale.wizard import forms
from tsuru_autoscale.wizard import client
from tsuru_autoscale.datasource import client as dclient
import requests
import urllib


def get_or_create_tsuru_instance(instance_name, token):
    token = urllib.unquote(token)
    token = "bearer {}".format(token)
    url = "{}/services/autoscale/instances/{}".format(client.tsuru_host(), instance_name)
    headers = {"Authorization": token}
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        return

    app = client.app_info(instance_name, token)
    url = "{}/services/autoscale/instances".format(client.tsuru_host(), instance_name)
    headers = {"Authorization": token, "Content-Type": "application/x-www-form-urlencoded"}
    data = {"service_name": "autoscale", "name": instance_name, "owner": app["teamowner"]}
    response = requests.post(url, headers=headers, data=data)

    url = "{}/services/{}/instances/{}/{}".format(client.tsuru_host(), "autoscale", instance_name, instance_name)
    headers = {"Authorization": token}
    response = requests.put(url, headers=headers, data={"noRestart": "true"})


def new(request, instance=None):
    token = request.session.get('tsuru_token').split(" ")[-1]

    dlist = [(d["Name"], d["Name"]) for d in dclient.list(token).json()]

    scale_up_form = forms.ScaleForm(30, request.POST or None, prefix="scale_up", initial={"operator": ">"})
    scale_up_form.fields['metric'].choices = dlist


# Path: tsuru_autoscale/wizard/forms.py
# OPERATOR_CHOICES = (
#     ('>', '>'),
#     ('>=', '>='),
#     ('<=', '<='),
#     ('<', '<'),
#     ('!=', '!='),
# )
# AGGREGATOR_CHOICES = (
#     ('avg', 'average'),
#     ('max', 'maximum'),
# )
# class ScaleForm(forms.Form):
# class ConfigForm(forms.Form):
#     def __init__(self, min_wait, *args, **kwargs):
#     def clean_value(self):
#     def clean_step(self):
# 
# Path: tsuru_autoscale/wizard/client.py
# def host():
# def tsuru_host():
# def clean_token(token):
# def app_info(name, token):
# def pool_info(name, token):
# def process_list(instance_name, token):
# def new(data, token):
# def get(name, token):
# def remove(name, token):
# def enable(name, token):
# def disable(name, token):
# def events(name, token):
# 
# Path: tsuru_autoscale/datasource/client.py
# def host():
# def new(data, token):
# def list(token):
# def remove(name, token):
# def get(name, token):


# Path: tsuru_autoscale/wizard/views.py
from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from django.contrib import messages
from tsuru_autoscale.wizard import forms
from tsuru_autoscale.wizard import client
from tsuru_autoscale.datasource import client as dclient
import requests
import urllib



def get_or_create_tsuru_instance(instance_name, token):
    token = urllib.unquote(token)
    token = "bearer {}".format(token)
    url = "{}/services/autoscale/instances/{}".format(client.tsuru_host(), instance_name)
    headers = {"Authorization": token}
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        return

    app = client.app_info(instance_name, token)
    url = "{}/services/autoscale/instances".format(client.tsuru_host(), instance_name)
    headers = {"Authorization": token, "Content-Type": "application/x-www-form-urlencoded"}
    data = {"service_name": "autoscale", "name": instance_name, "owner": app["teamowner"]}
    response = requests.post(url, headers=headers, data=data)

    url = "{}/services/{}/instances/{}/{}".format(client.tsuru_host(), "autoscale", instance_name, instance_name)
    headers = {"Authorization": token}
    response = requests.put(url, headers=headers, data={"noRestart": "true"})


def new(request, instance=None):
    token = request.session.get('tsuru_token').split(" ")[-1]

    dlist = [(d["Name"], d["Name"]) for d in dclient.list(token).json()]

    scale_up_form = forms.ScaleForm(30, request.POST or None, prefix="scale_up", initial={"operator": ">"})
    scale_up_form.fields['metric'].choices = dlist


    scale_down_form = forms.ScaleForm(60, request.POST or None, prefix="scale_down", initial={"operator": "<"})