import math

from random import SystemRandom
from flask import jsonify
from app import app


def generate_image_completion(rand_func):
  images = {}
  for image_id in range(rand_func.randint(1, 11)):
    total = int(math.pow(abs(rand_func.gauss(0, 1000)), 2))
    current = rand_func.randint(0, total)
    image_id = 'image_id_%s' % image_id
    images[image_id] = {
      'total': total,
      'current': current,
    }
  return images


@app.route('/test/build/status', methods=['GET'])
def generate_random_build_status():
  response = {
    'id': 1,
    'total_commands': None,
    'current_command': None,
    'push_completion': 0.0,
    'status': None,
    'message': None,
    'image_completion': {},
  }

  random = SystemRandom()
  phases = {
    'waiting': {},
    'starting': {
      'total_commands': 7,
      'current_command': 0,
    },
    'initializing': {},
    'error': {
      'message': 'Oops!'
    },
    'complete': {},
    'building': {
      'total_commands': 7,
      'current_command': random.randint(1, 7),    
    },
    'pushing': {
      'total_commands': 7,
      'current_command': 7,
      'push_completion': random.random(),
      'image_completion': generate_image_completion(random),
    },
  }

  phase = random.choice(phases.keys())
  response['status'] = phase
  response.update(phases[phase])

  return jsonify(response)