# coding: utf-8
from time import sleep

from jenkinsapi.jenkins import Jenkins

from slackbot import settings
from slackbot.bot import respond_to, listen_to


stop_stage_build_flag = False
build_in_progress = False


@respond_to('stage build')
def stage_build(message):
    global stop_stage_build_flag
    global build_in_progress

    build_in_progress = True
    stop_stage_build_flag = False

    for i in xrange(5):
        if stop_stage_build_flag:
            message.send('>Stopping stage build')
            break
        else:
            message.send('>Stage build in {0}'.format(5 - i))
            sleep(10)
    if not stop_stage_build_flag:
        message.send('>FIRE!!! 🔥')
        J = Jenkins(settings.JENKINS_URL,
                    username=settings.JENKINS_USERNAME,
                    password=settings.JENKINS_PASSWORD)
        my_job = J['stage']
        res = my_job.invoke()
        message.send('>Started build {0}'.format(res))
    else:
        message.send('>Stage build was halted')
    build_in_progress = False


@listen_to('fuck')
@listen_to('no build')
@listen_to('ohrana otmena')
@listen_to('nonono')
@respond_to('stop')
def stop_stage_build(message):
    global stop_stage_build_flag
    stop_stage_build_flag = True

@@ -522,32 +522,64 @@
 k%0A        else:%0A
+            if i in %5B0, 4%5D:%0A    
             mess
@@ -603,24 +603,28 @@
 build in %7B0%7D
+ sec
 '.format(5 -
@@ -625,12 +625,18 @@
 at(5
+0
  - i
+ * 10
 ))%0A 
@@ -694,43 +694,8 @@
 ag:%0A
-        message.send('%3EFIRE!!! %F0%9F%94%A5')%0A
     
@@ -925,16 +925,19 @@
 .send('%3E
+ %F0%9F%94%A5 
 Started 

Send less messages