import random
import time
import ui
import common

log = common.get_logger("apps")

class Rule:
    def __init__(self):
        pass

    @classmethod
    def count(cls, num, interval, factor = 0):
        rule = cls()
        rule.num = num
        rule.interval = interval
        rule.factor = factor
        return rule

    @classmethod
    def time(cls, duration, interval, factor = 0):
        rule = cls()
        rule.duration = duration
        rule.interval = interval
        rule.factor = factor
        return rule

class _App:
    def __init__(self, name):
        self.name = name

    def name(self):
        return self.name
    
    def open_app(self):
        pass

    def open_function(self):
        pass

class Task:
    def __init__(self, name, rule, func):
        self.name = name
        self.rule = rule
        self.func = func

    @classmethod
    def watch_video(cls, name, rule, device):
        return cls(name, rule, lambda : device.swipe_up(150, 40))
    
    @classmethod
    def read_book(cls, name, rule, device):
        return cls(name, rule, lambda : device.swipe_left(200, 60))
    
    @classmethod
    def browse(cls, name, rule, device):
        return cls(name, rule, lambda : device.swipe_up(300, 300))

    def start(self):
        log.info("开始执行任务：{}".format(self.name))
        _exec(self.rule, self.func, self.name)
        log.info("任务{}执行完毕".format(self.name))
        pass

def _exec(rule, func = [], log_prefix = "执行任务"):
    factor = getattr(rule, "factor")
    if hasattr(rule, "num"):
        num = getattr(rule, "num")
        i = 1
        while i <= num:
            wait_time = getattr(rule, "interval")
            if factor > 0:
                wait_time = round(random.uniform(wait_time - factor, wait_time + factor), 2)
            log.info("{}：{}/{} ，停留 {} 秒".format(log_prefix, i, num, wait_time))
            if isinstance(func, list):
                for f in func:
                    f()
            else:
                func()
            time.sleep(wait_time)
            i += 1
        return True
    
    if hasattr(rule, "time"):
        duration = getattr(rule, "duration")
        time_total = 0
        while time_total < duration:
            wait_time = getattr(rule, "interval")
            if factor > 0:
                wait_time = round(random.uniform(wait_time - factor, wait_time + factor), 2)
            if time_total + wait_time > duration:
                wait_time = round(duration - time_total, 2)
            log.info("{}：{}/{} ，停留 {} 秒".format(log_prefix, time_total, duration, wait_time))
            func()
            time.sleep(wait_time)
            time_total = round(time_total + wait_time, 2)
        return True
    
    return False

class ToutiaoLite(_App):
    def __init__(self, device):
        _App.__init__(self, "头条极速版")
        self.device = device

    def open_treasure_box(action):
        log.info("开宝箱...")
        log.info("开宝箱完成...")

    # 看广告
    def func_ad(self):
        button = ui.Button(240, 450, 850, 920)
        point = button.get_point()
        self.device.back()
        self.device.back() # 防止自动进入直播界面
        self.device.click(point)
    
class DouyinLite(_App):
    def __init__(self, device):
        _App.__init__(self, "抖音极速版")
        self.device = device

class KuaishouLite(_App):
    def __init__(self, device):
        _App.__init__(self, "快手极速版")
        self.device = device

class TomatoListen(_App):
    def __init__(self, device):
        _App.__init__(self, "番茄畅听")
        self.device = device

    # 看广告
    def func_ad(self):
        button = ui.Button(200, 850, 500, 900)
        point = button.get_point()
        self.device.back()
        self.device.back() # 防止自动进入直播界面
        self.device.click(point)

class TomatoFiction(_App):
    def __init__(self, device):
        _App.__init__(self, "番茄小说")
        self.device = device

class Alipay(_App):
    def __init__(self, device):
        _App.__init__(self, "支付宝")
        self.device = device
