/* 设置UI模式 */
"ui";
/* ui界面 */
ui.layout(
    <vertical>
        <appbar>
            <toolbar id="headTitle" title="鲨皇撸猫猫小助手" />
            <tabs id="tabs" />
        </appbar>
        <card margin="10 10 10 5">
            <text text="本软件只用于学习讨论，请下载后24小时内删除" margin="10 15 5 15" textColor="red" />
        </card>
        <card margin="10 10 10 5">
            <text text="请先开启无障碍服务、悬浮框权限再使用撸猫猫小助手，滑动时间设置10-20秒之间" margin="10 15 5 15" />
        </card>
        <card margin="10 10 10 5">
            <vertical>
                <Switch id="accessibleService" text="无障碍服务" checked="{{auto.serivce != null}}" margin="10 10 5 10" />
                <Switch id="floatingWindow" text="悬窗权限" layout_gravity="center" margin="10 10 8 10" />
            </vertical>
        </card>
        <card margin="10 10 10 5">
            <horizontal>
                <text text="滑动时间" marginLeft="10" />
                <input id="setTime" text="10" textColor="grey" w="40" gravity="center" />
                <text text="秒" />
                <text text="喂猫次数" marginLeft="100" />
                <input id="eatCat" text="1" textColor="grey" w="40" gravity="center" />
                <text text="次" />
            </horizontal>
        </card>
        <button id="starts" text="开始撸猫猫" style="Widget.AppCompat.Button.Colored" margin="6 0 6 0" padding="18" />
    </vertical>
);
var getDeviceWidth = device.width;//获取设备的宽度
var getDeviceHeight = device.height;//获取设备的高度
var getInputTime = ui.setTime.text() * 1000;
setScreenMetrics(getDeviceWidth, getDeviceHeight);//按设备的宽、高度适配
/* 无障碍服务 */
ui.accessibleService.on("check", function (checked) {
    // 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
    if (checked && auto.service == null) {
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
    if (!checked && auto.service != null) {
        auto.service.disableSelf();
    }
});
/* 根据无障碍服务的开启情况，同步开关的状态 */
ui.emitter.on("resume", function () {
    ui.accessibleService.checked = auto.service != null;
});
/* 悬浮窗权限 */
ui.floatingWindow.on("check", function (checked) {
    // 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
    if (auto.service != null) {
        if (checked) {
            int = app.startActivity({
                packageName: "com.android.settings",
                className: "com.android.settings.Settings$AppDrawOverlaySettingsActivity",
                data: "package:" + auto.service.getPackageName().toString()
            });
        }
    } else {
        toastLog("请先开启无障碍服务");
        ui.floatingWindow.checked = false;
    }
});
/* 开始执行 */
ui.starts.on("click", function () {
    if (ui.accessibleService.checked == false && auto.service == null) {
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
        toastLog("请先开启无障碍服务");
    } else {
        threads.start(function () {
            monitoring();
        })
        threads.start(function () {
            console.show();
            console.setPosition(getDeviceWidth / 2.8, getDeviceHeight / 100);//按设备分辨率设置控制台在右上角
            start();
            log("已完成所有任务");
            sleep(888);
            log("检测是否还有剩余任务未完成");
            for (var i = 0; i <= 2; i++) {
                text("关闭").findOne().click();
                sleep(1688);
                findTake();
            }
            log("已完成所有任务");
            text("关闭").findOne().click();
            sleep(888);
            if (ui.eatCat.text() != null) {
                log("开始喂猫");
                textContains("喂猫升级").findOne().click();
                sleep(1888);
                if (text("做任务赚喵币").exists()) {
                    log("已经没有喵币了");
                    text("关闭").findOne().click();
                    sleep(1688);
                } else {
                    while (!text("做任务赚喵币").exists()) {
                        for (var i = 0; i < ui.eatCat.text(); i++) {
                            textContains("喂猫升级").findOne().click();
                            sleep(1888);
                        }
                        while (textContains("选兴趣").exists()) {
                            textContains("选兴趣").findOne().click();
                            sleep(1888);
                        }
                        // if (text("做任务赚喵币").exists()) {
                        //     log("已经没有喵币了");
                        //     text("关闭").findOne().click();
                        //     sleep(888);
                        //     log("恭喜发财");
                        //     break;
                        // }
                    }
                }
            }
        })
    }
});
/* 进入撸猫猫 */
function start() {
    console.clear();
    log("欢迎使用撸猫猫小助手")
    home();
    sleep(1688);
    launch("com.taobao.taobao");
    sleep(5888);
    desc("搜索").findOne().click();
    sleep(888)
    setText("超级星秀猫");
    sleep(888)
    text("搜索").findOne().click();
    sleep(5888);
    waitForActivity("com.taobao.browser.BrowserActivity");
    log("达到撸猫猫")
    sleep(5888);
    if (textEndsWith("喵币点击领取").exists()) {
        textEndsWith("喵币点击领取").findOne().click();
        sleep(2888);
        log("领取喵币");
        sleep(1688);
        findTake();
    } else {
        log("今天已领取喵币");
        sleep(1688);
        findTake();
    }

}
/* 查找任务 */
function findTake() {
    if (text("赚喵币").exists()) {
        text("赚喵币").findOne().click();
        sleep(2888);
        if (textContains("每日签到").exists()) {
            var today = textContains("每日签到").findOne().bounds();
            click(today.centerX(), today.centerY());
            log("签到成功");
            sleep(2888);
        }
        sleep(2888);
        var count = 0;
        while (text("去浏览").exists()) {
            count++;
            text("去浏览").findOne().click();
            sleep(1688);
            getSwipe(random(888, 1688));
            sleep(3888);
            getSwipe(random(666, 1088));
            sleep(5888);
            getSwipe(random(333, 888));
            sleep(getInputTime);
            log("完成第" + count + "浏览");
            sleep(888);
            back();
            sleep(1688);
        }
        sleep(1688);
        while (text("去搜索").exists()) {
            count++;
            text("去搜索").findOne().click();
            sleep(1688);
            getSwipe(random(888, 1688));
            sleep(3888);
            getSwipe(random(666, 1088));
            sleep(5888);
            getSwipe(random(333, 888));
            sleep(getInputTime);
            log("完成第" + count + "浏览");
            sleep(888);
            back();
            sleep(1688);
        }
        sleep(1688);
        while (text("去逛逛").exists()) {
            count++;
            text("去逛逛").findOne().click();
            sleep(1688);
            getSwipe(random(888, 1688));
            sleep(3888);
            getSwipe(random(666, 1088));
            sleep(5888);
            getSwipe(random(333, 888));
            sleep(getInputTime);
            log("完成第" + count + "浏览");
            sleep(888);
            back();
            sleep(1688);
        }
        sleep(1688);
        while (text("去施肥").exists()) {
            count++;
            text("去施肥").findOne().click();
            sleep(5888);
            click(770, 1300);
            sleep(666);
            click(770, 1300);
            sleep(1688);
            click(540, 1650);
            sleep(1688);
            text("去集肥料").findOne().click();
            sleep(1688);
            text("去签到").findOne().click();
            sleep(2888);
            text("关闭").findOne().click();
            sleep(1688);
            click(540, 1650);
            sleep(1688);
            back();
            sleep(2888);
        }
        sleep(1688);
        if (textContains("邀请好友一起").exists()) {
            sleep(1688)
            while (text("去完成").findOnce(1)) {
                count++;
                text("去完成").findOnce(1).click();
                getSwipe(random(888, 1688));
                sleep(3888);
                getSwipe(random(666, 1088));
                sleep(5888);
                getSwipe(random(333, 888));
                sleep(getInputTime);
                log("完成第" + count + "浏览");
                sleep(888);
                back();
                sleep(1688);
            }
        } else {
            sleep(1688)
            while (text("去完成").findOnce(0)) {
                count++;
                text("去完成").findOnce(0).click();
                getSwipe(random(888, 1688));
                sleep(3888);
                getSwipe(random(666, 1088));
                sleep(5888);
                getSwipe(random(333, 888));
                sleep(getInputTime);
                log("完成第" + count + "浏览");
                sleep(888);
                back();
                sleep(1688);
            }
        }
    }
}
/* 滑动模块 */
function getSwipe(int) {
    sml_move(getDeviceWidth / random(3, 5), getDeviceHeight - random(100, 300), getDeviceWidth / random(1.2, 2), getDeviceHeight / random(6, 20), int);
    function bezier_curves(cp, t) {
        cx = 3.0 * (cp[1].x - cp[0].x);
        bx = 3.0 * (cp[2].x - cp[1].x) - cx;
        ax = cp[3].x - cp[0].x - cx - bx;
        cy = 3.0 * (cp[1].y - cp[0].y);
        by = 3.0 * (cp[2].y - cp[1].y) - cy;
        ay = cp[3].y - cp[0].y - cy - by;
        tSquared = t * t;
        tCubed = tSquared * t;
        result = {
            "x": 0,
            "y": 0
        };
        result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
        result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
        return result;
    };
    //仿真随机带曲线滑动  
    //qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
    function sml_move(qx, qy, zx, zy, time) {
        var xxy = [time];
        var point = [];
        var dx0 = {
            "x": qx,
            "y": qy
        };
        var dx1 = {
            "x": random(qx - 100, qx + 100),
            "y": random(qy, qy + 50)
        };
        var dx2 = {
            "x": random(zx - 100, zx + 100),
            "y": random(zy, zy + 50),
        };
        var dx3 = {
            "x": zx,
            "y": zy
        };
        for (var i = 0; i < 4; i++) {
            eval("point.push(dx" + i + ")");
        };
        for (let i = 0; i < 1; i += 0.08) {
            xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]
            xxy.push(xxyy);
        }
        gesture.apply(null, xxy);
    };
}
/* 监控线程 */
function monitoring() {
    while (true) {
        if (textContains("我知道了").exists()) {
            textContains("我知道了").findOne().click();
        }
        if (textContains("开心收下").exists()) {
            textContains("开心收下").findOne().click();
        }
        if (text("领取奖励").exists()) {
            text("领取奖励").findOne().click();
        }
    }
}