import os
import re
import sys
import time
import glob
import urllib2
import base64

from magnolia.utility import *
from magnolia.utility import LOG as L
from magnolia.script.kancolle import testcase_base

def find_all_files(directory):
    for root, dirs, files in os.walk(directory):
        yield root
        for f in files:
            yield os.path.join(root, f)

class TestCase_Normal(testcase_base.TestCase_Base):
    def __init__(self, *args, **kwargs):
        super(TestCase_Normal, self).__init__(*args, **kwargs)

    def quest_search(self, _id):
        for f in find_all_files(self.get_base("quest")):
            if _id in str(f):
                dst = f.replace("%s\\" % self.get_base("quest"), "")
                L.info("%s -> %s" % (str(f), dst))
                return dst

    def quest(self, _id):
        if not self.search("home"): return False
        self.tap("home\\quest"); self.sleep()
        self.tap_wait("quest"); self.sleep()
        self.quest_done(); self.sleep()
        if not self.search("quest\\mission"):
            self.tap_wait("quest\\return"); self.sleep()
            self.wait("home")
            return False
        pm_path = "quest\\%s" % self.quest_search(_id)
        L.info(pm_path)
        if "daily" in pm_path: self.tap("quest\\daily"); self.sleep(4)
        elif "weekly" in pm_path: self.tap("quest\\weekly"); self.sleep(4)
        if not self.quest_check("quest\\acceptance", pm_path):
            self.tap_wait("quest\\return"); self.sleep()
            self.wait("home")
            return False
        if not self.search("quest\\mission", count=5):
            self.tap_wait("quest\\return"); self.sleep()
            self.wait("home")
            return False
        if not self.search("quest\\perform_select"):
            self.tap("quest\\perform"); self.sleep()
        self.upload("quest_%s" % self.adb.get().TMP_PICTURE)
        self.tap_wait("quest\\return"); self.sleep()
        return self.wait("home")

    def battle_2_3(self):
        if not self.search("attack\\compass_b"):
            if self.search("home"): return True
            else: return False
        while not self.search("home"):
            while not self.search("basic\\next"):
                if self.tap("attack\\compass"): self.sleep(10)
                if self.tap("attack\\formation\\1"): self.sleep(10)
                if self.tap("attack\\night_battle\\start"): self.sleep(15)
                if self.search("attack\\get"):
                    self.tap("attack\\return"); self.sleep(5)
                    return self.search("home")
            if self.tap("basic\\next"): self.sleep(5)
            nextstage = "attack\\charge"
            if self.search("attack\\result_damage"):
                nextstage = "attack\\withdrawal"
            while self.tap("basic\\next"): self.sleep(5)
            if self.search("home"): break
            while not self.search(nextstage):
                if self.search("basic\\next"):
                    self.upload("drop_%s.png" % self.adb.get().SERIAL)
                    self.tap("basic\\next")
            self.tap(nextstage); time.sleep(5)
        self.slack_message(self.get("bot.attack_return"))
        return self.search("home")

    def battle_all_stage(self, formation, withdrawal=False):
        if not self.search("attack\\compass_b"):
            if self.search("home"): return True
            else: return False
        while not self.search("home"):
            while not self.search("basic\\next"):
                if self.tap("attack\\compass"): self.sleep(10)
                if self.tap("attack\\formation\\%s" % formation): self.sleep(10)
                if self.tap("attack\\night_battle\\start"): self.sleep(15)
                if self.search("attack\\get"):
                    self.tap("attack\\return"); self.sleep(5)
                    return self.search("home")
                self.sleep(10)
            if self.tap("basic\\next"): self.sleep(5)
            nextstage = "attack\\charge"
            if withdrawal or self.search("attack\\result_damage"):
                nextstage = "attack\\withdrawal"
            while self.tap("basic\\next"): self.sleep(5)
            if self.search("home"): break
            while not self.search(nextstage):
                if self.search("basic\\next"):
                    self.upload("drop_%s.png" % self.adb.get().SERIAL)
                    self.tap("basic\\next")
            self.tap(nextstage); time.sleep(5)
        self.slack_message(self.get("bot.attack_return"))
        return self.search("home")

    def call_job(self, _id, timeout=1800):
        L.info("Call %s : %s" % (_id, self.invoke_jenkins_job(_id, timeout)))

    def call_job_tree(self, _id, timeout=1200):
        if _id == "DB01":
            self.call_job("DB02", timeout)
        elif _id == "DB02":
            self.call_job("DB03", timeout); self.call_job("DB04", timeout)
            self.call_job("WB01", timeout); self.call_job("WB02", timeout * 1.5)
        elif _id == "DB04":
            self.call_job("DB05", timeout); self.call_job("WB03", timeout * 1.5)
        elif _id == "DB05":
            self.call_job("DB06", timeout)
        elif _id == "WB01":
            self.call_job("WB04", timeout * 1.5)
        elif _id == "WB03":
            self.call_job("WB05", timeout * 1.5)
        else:
            L.info("Not Call.")
