"""
Views show job status.

**Component**: views
  :Macro: views
  :Entry Point: jenkins_jobs.views
"""


import xml.etree.ElementTree as XML
import jenkins_jobs.modules.base


def all_view(parser, xml_parent, data):
    """
    All view

    :arg bool filter-executors: only those build executors will be shown that
      could execute the jobs in this view.
    :arg bool filter-queue: only jobs in this view will be shown in the queue.
    :arg bool folder: Wether or not this view is in a folder.


    Example:

    .. literalinclude::  /../tests/views/fixtures/all_view.yaml

    """
    view = XML.SubElement(xml_parent, 'hudson.model.AllView')
    XML.SubElement(view, 'name').text = 'All'
    in_folder = data.get('folder', False)
    owner_attrs = dict()
    if in_folder:
        owner_attrs['class'] = 'com.cloudbees.hudson.plugins.folder.Folder'
        owner_attrs['reference'] = '../../..'
        XML.SubElement(view, 'owner', attrib=owner_attrs)

    executors = data.get('filter-executors', False)
    XML.SubElement(view, 'filterExecutors').text = str(executors).lower()

    queue = data.get('filter-queue', False)
    XML.SubElement(view, 'filterQueue').text = str(queue).lower()

    properties_attributes = dict()
    properties_attributes['class'] = 'hudson.model.View$PropertyList'
    XML.SubElement(view, 'properties', attrib=properties_attributes)


def delivery_pipeline_view(parser, xml_parent, data):
    """
    Delivery Pipeline View requires the Jenkins `Delivery Pipeline Plugin.
    <https://wiki.jenkins-ci.org/display/JENKINS/Delivery+Pipeline+Plugin>`_

    :arg bool filter-executors: only those build executors will be shown that
      could execute the jobs in this view.
    :arg bool filter-queue: only jobs in this view will be shown in the queue.
    :arg bool folder: Wether or not this view is in a folder.
    :arg str name: The name of this view.
    :arg dict components: The components (jobs) for this pipeline:

        * **name** (str): Name of the pipeline, usually the name of the
                          component or product.
        * **first-job** (str): First job in the pipeline. Usually the
                             build/compile job. The build number/build
                             display name will be used as the version in
                             later tasks or stages. If using folders, it
                             should be a full path to the job.

    :arg int number-of-pipelines: Number of pipelines instances shown for each
                                  pipeline.
    :arg bool show-aggregated-pipeline: Show an aggregated view where each
                                        stage shows the latest version being
                                        executed.

    :arg int number-of-columns: Number of columns used for showing pipelines.
                                Useful for multiple components in the view to
                                show them beside each others.

    :arg int sorting: How to sort the pipeline in the view.
                      Only applicable for several pipelines.
                      Can be sorted by latest activity or by name.
    :arg int update-interval: How often will the view be updated in seconds.

    :arg bool allow-pipeline-start: Start a new pipeline build.

    :arg bool allow-manual-triggers: If a task is manual (Build other projects
      (manual step) from Build Pipeline Plugin, show a button.

    :arg bool allow-rebuild: Rebuild a task.

    :arg str show-avatars: Show avatars pictures instead of names of the people
      involved in a pipeline instance. Use the `Avatar Plugin
      <https://https://wiki.jenkins-ci.org/display/JENKINS/Avatar+Plugin>`_
      or the `Gravatar Plugin.
      <https://wiki.jenkins-ci.org/display/JENKINS/Gravatar+plugin>`_ or
      similar to set avatar picture for contributors.

    :arg bool show-changes: Show SCM change log for the first job in the
      pipeline. If Repository browser is configured, link to change will be
      created to the repository browser.

    :arg bool show-description: Show build description connected to a task.

    :arg bool show-promotions: Show promotions from the `Promoted Builds
      Plugin.
      <https://wiki.jenkins-ci.org/display/JENKINS/Promoted+Builds+Plugin>_`

    :arg bool show-total-buildtime: Show total build time of a pipeline.
      If there are multiple routes in a pipeline, total build time is
      calculated as the sum of the build times in the longest route.

    :arg str css-url: Possibility to override CSS for the normal view.
      Enter the full url to the custom CSS.

    :arg str fullscreen-css-url: Possibility to override CSS for the
      fullscreen view. Enter the full url to the custom CSS.

    :arg list regexp-first-jobs: Find jenkins job matching regular expression.
      ^build-(.+?)-project

    Example:

    .. literalinclude::  /../tests/views/fixtures/delivery_pipeline.yaml

    """
    delivery_pipeline = 'se.diabol.jenkins.pipeline.DeliveryPipelineView'

    view = XML.SubElement(xml_parent, delivery_pipeline)
    in_folder = data.get('folder', False)
    owner_attrs = dict()
    if in_folder:
        owner_attrs['class'] = 'com.cloudbees.hudson.plugins.folder.Folder'
        owner_attrs['reference'] = '../../..'
    XML.SubElement(view, 'owner', attrib=owner_attrs)

    XML.SubElement(view, 'name').text = data.get('name')

    executors = data.get('filter-executors', False)