package cn.com.ctop.crawler.modules.core.util;

import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;
import java.time.Duration;
import java.util.List;
import java.util.Map;

@Slf4j
public class AppiumUtil {
    private Boolean isLoop = true;
    private Duration duration = Duration.ofSeconds(1);

    public static final String FIND_ELEMENT_TYPE_ID = "id";
    public static final String FIND_ELEMENT_TYPE_XPATH = "xpath";
    public static final String FIND_ELEMENT_TYPE_CLASS = "class";
    public static final String APP_SWIPE_TYPE_UP = "up";
    public static final String APP_SWIPE_TYPE_DOWN = "down";
    public static final String APP_SWIPE_TYPE_LEFT = "left";
    public static final String APP_SWIPE_TYPE_RIGHT = "right";

    public void swipeToUp(AndroidDriver driver) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction action1 = new TouchAction(driver).press(PointOption.point(width / 2, height * 3 / 4)).waitAction(WaitOptions.waitOptions(duration))
                .moveTo(PointOption.point(width / 2, height / 4)).release();
        action1.perform();
    }

    public void swipeToDown(AndroidDriver driver) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction action2 = new TouchAction(driver).press(PointOption.point(width / 2, height / 4)).waitAction(WaitOptions.waitOptions(duration))
                .moveTo(PointOption.point(width / 2, height * 3 / 4)).release();
        action2.perform();
    }

    public void swipeToLeft(AndroidDriver driver) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction action3 = new TouchAction(driver).press(PointOption.point(width * 3 / 4, height / 2)).waitAction(WaitOptions.waitOptions(duration))
                .moveTo(PointOption.point(width / 4, height / 2)).release();
        action3.perform();
    }

    public void tapPoint(AndroidDriver driver, Integer x, Integer y) {
        TouchAction action = new TouchAction(driver).tap(PointOption.point(x, y)).release();
        action.perform();
    }

    public void swipeToRight(AndroidDriver driver) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction action4 = new TouchAction(driver).press(PointOption.point(width / 4, height / 2)).waitAction(WaitOptions.waitOptions(duration))
                .moveTo(PointOption.point(width * 3 / 4, height / 2)).release();
        action4.perform();
    }

    public AndroidDriver initAndroidDriver(String serverUrl, String deviceName, String appPackage, String appActivity) throws Exception {
        return new AndroidDriver(new URL(serverUrl), initCapabilities(deviceName, appPackage, appActivity));
    }

    public DesiredCapabilities initCapabilities(String deviceName, String appPackage, String appActivity) {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("deviceName", deviceName);
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("appPackage", appPackage);
        desiredCapabilities.setCapability("noReset", true);
        desiredCapabilities.setCapability("unicodeKeyboard", true);
        desiredCapabilities.setCapability("resetKeyboard", true);
        desiredCapabilities.setCapability("appActivity", appActivity);
        return desiredCapabilities;
    }

    public static void main(String[] args) {
        AppiumUtil appiumUtil = new AppiumUtil();
        try {
//            AndroidDriver androidDriver = appiumUtil.initAndroidDriver("http://39.106.184.70:4723/wd/hub", "183.131.157.66:10005", "com.smile.gifmaker", "com.yxcorp.login.userlogin.QuickLoginActivity");
            AndroidDriver androidDriver = appiumUtil.initAndroidDriver("http://39.106.184.70:4723/wd/hub", "183.131.157.66:10005", "com.smile.gifmaker", "com.yxcorp.gifshow.HomeActivity");
//            appiumUtil.loginKuaishou(androidDriver);
            appiumUtil.loginTask(androidDriver, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Boolean loginTask(AndroidDriver androidDriver, String settoken, String setmobile) throws Exception {
        Thread.sleep(5000);
        String token = settoken;
        String mobile = setmobile;
        String pkg = androidDriver.getCurrentPackage();
        String activity = androidDriver.currentActivity();
        Boolean isContinue = true;
        if ("com.android.packageinstaller".equals(pkg)) {
            switch (activity) {
                case ".permission.ui.GrantPermissionsActivity":
                    WebElement permissionAllowBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.android.packageinstaller:id/permission_allow_button");
                    if (permissionAllowBtn != null) {
                        permissionAllowBtn.click();
                    }
                    break;
                case ".PackageInstallerActivity":
                    WebElement okBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.android.packageinstaller:id/ok_button");
                    if (okBtn != null) {
                        okBtn.click();
                    }
                    break;
                case ".InstallAppProgress":
                    WebElement centerText = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.android.packageinstaller:id/center_text");
                    WebElement doneBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.android.packageinstaller:id/done_button");

                    if (centerText != null) {
                        log.info(centerText.getText());
                    }
                    if (doneBtn != null) {
                        doneBtn.click();
                    }
                    break;
                default:
                    isContinue = false;
                    androidDriver.quit();
                    log.info("当前activity无法处理");
                    break;
            }
        } else if ("com.smile.gifmaker".equals(pkg)) {
            //干扰项
            WebElement positive = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/positive");
            if (positive != null) {
                positive.click();
            }

            switch (activity) {
                //干扰项
                case "com.yxcorp.gifshow.webview.KwaiWebViewActivity":
                    WebElement leftBtn1 = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/left_btn");
                    if (leftBtn1 != null) {
                        leftBtn1.click();
                    }
                    break;
                //TODO --- 首
                // 1页
                case "com.yxcorp.gifshow.HomeActivity":

                    WebElement loginText = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/login_text");
                    if (loginText != null) {
                        loginText.click();
                        break;
                    }
                    WebElement leftText = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/left_text");
                    if (leftText != null) {
                        leftText.click();
                        break;
                    }
                    WebElement phoneLoginBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/phone_login_view");
                    if (phoneLoginBtn != null) {
                        phoneLoginBtn.click();
                        break;
                    }
                    WebElement closeBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/close_btn");
                    if (closeBtn != null) {
                        closeBtn.click();
                        break;
                    }
                    WebElement leftBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/left_btn");
                    if (leftBtn != null) {
                        leftBtn.click();
                        Thread.sleep(5000);
                        //查询左侧是否存在头像
                        WebElement tabAvatar = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/tab_avatar");
                        if (tabAvatar != null) {
                            tabAvatar.click();
                        } else {
                            //不存在头像
                            Thread.sleep(3000);
                            tapPoint(androidDriver, 650, 1230);
                            Thread.sleep(3000);
                            // 点击我的
                            tapPoint(androidDriver, 650, 1230);
                            Thread.sleep(3000);
                            tapPoint(androidDriver, 650, 400);
                        }
                    }
                    break;
                case "com.yxcorp.gifshow.profile.activity.MyProfileActivity":
                    WebElement loadingTitle = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/loading_title");
                    if (loadingTitle != null) {
                    }
                    tapPoint(androidDriver, 650, 400);
                    break;
                //TODO ---- 获取快手ID
                case "com.yxcorp.plugin.qrcode.MyQRCodeActivity":
                    WebElement kwaiId = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/kwai_id");
                    if (kwaiId != null) {
                        isContinue = false;
                        androidDriver.quit();
                    }
                    break;
                //TODO ----
                case "com.yxcorp.gifshow.activity.BrowseSettingsActivity":
                    //com.smile.gifmaker:id/browse_setting_new_btn 开启大屏模式  关闭大屏模式
                    //com.smile.gifmaker:id/left_btn 返回
                    break;
                //TODO ----登录
                case "com.yxcorp.login.userlogin.LoginActivity":
                    WebElement loginTitleTv = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/login_title_tv");
                    WebElement loginNameEt = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/login_name_et");
                    WebElement nextView = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/next_view");
                    if (loginTitleTv != null) {
                        if ("输入密码".equals(loginTitleTv.getText())) {
                            WebElement forgetPsdBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/forget_psd_btn");
                            if (forgetPsdBtn != null) {
                                forgetPsdBtn.click();
                            }
                        }
                    }
                    if (loginNameEt != null) {
                        token = KuaimiUtil.kuaimiLogin();
                        mobile = KuaimiUtil.getMobile(token, "2307");
                        loginNameEt.sendKeys(mobile);
                        Thread.sleep(5000);
                        nextView.click();
                    }
                    break;
                //TODO ---- 重置密码
                case "com.yxcorp.login.userlogin.RetrievePasswordActivity":

                    break;
                //TODO
                case "com.yxcorp.login.userlogin.ResetSelectedAccountPasswordActivity":
                    WebElement confirmBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/confirm_btn");
                    WebElement loginPsdEt = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/login_psd_et");
                    if (loginPsdEt != null) {
                        loginPsdEt.sendKeys("a123456");
                        Thread.sleep(5000);
                        if (confirmBtn != null) {
                            confirmBtn.click();
                        }
                    }
                    break;
                case "com.yxcorp.login.userlogin.MultiLoginAccountSelectActivity":
                    WebElement iconOne = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/icon_one");
                    if (iconOne != null) {
                        iconOne.click();
                    }
                    break;
                case "com.yxcorp.login.userlogin.RegisterUserInfoSettingActivity":
                    //com.smile.gifmaker:id/right_btn
                    WebElement rightBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/right_btn");
                    if (rightBtn != null) {
                        rightBtn.click();
                    }
                    break;
                case "com.yxcorp.gifshow.activity.InterestTagActivity":
                    WebElement closeIv = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/close_iv");
                    if (closeIv != null) {
                        closeIv.click();
                    }
                    break;
                case "com.yxcorp.login.userlogin.QuickLoginActivity":
                    WebElement phoneLoginView = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/phone_login_view");
                    if (phoneLoginView != null) {
                        phoneLoginView.click();
                    }
                    break;
                case "com.yxcorp.login.userlogin.FullScreenLoginActivity":
                    WebElement phoneOneKeyLoginBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/phone_one_key_login_btn");
                    if (phoneOneKeyLoginBtn != null) {
                        phoneOneKeyLoginBtn.click();
                    }
                    break;
                //TODO ----
                case "com.yxcorp.login.userlogin.CaptchaCodeLoginActivity":
                    WebElement captchaEt = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/captcha_et");
                    if (captchaEt != null) {
                        int i = 0;
                        String msg = getMobileCaptcha(token, mobile);
                        if (msg != null) {
                            captchaEt.sendKeys(msg);
                            Thread.sleep(5000);
                            WebElement captchaFinish = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/captcha_finish");
                            if (captchaFinish != null) {
                                captchaFinish.click();
                            }
                        }
                    }
                    break;
                //TODO
                case "com.yxcorp.login.userlogin.PhoneLoginV2Activity":
                    WebElement phoneEt = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/phone_et");
                    if (phoneEt != null) {
                        token = KuaimiUtil.kuaimiLogin();
                        mobile = KuaimiUtil.getMobile(token, "2307");
                        phoneEt.sendKeys(mobile);
                        Thread.sleep(5000);
                        WebElement fetchCodeTv = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/fetch_code_tv");
                        if (fetchCodeTv != null) {
                            fetchCodeTv.click();
                            String msg = getMobileCaptcha(token, mobile);
                            if (msg != null) {
                                WebElement captchaCodeEt = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/captcha_code_et");
                                if (captchaCodeEt != null) {
                                    captchaCodeEt.sendKeys(msg);
                                    Thread.sleep(5000);
                                    WebElement cfmBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/confirm_btn");
                                    if (cfmBtn != null) {
                                        cfmBtn.click();
                                    }
                                }
                            }
                        }
                    }
                    break;
                default:
                    isContinue = false;
                    break;
            }
        } else if ("cn.com.longene.www.myapplication".equals(pkg)) {
            switch (activity) {
                case ".MainActivity":
                    isContinue = false;
                    break;
                default:
                    isContinue = false;
                    androidDriver.quit();
                    break;
            }
        } else {
        }
        if (isContinue) {
            isContinue = loginTask(androidDriver, token, mobile);
        }
        return isContinue;
    }

    public Map<String, Object> checkMethod(AndroidDriver androidDriver, String activity, Map<String, Object> result) throws Exception {
        Integer code = (Integer) result.get("code");
        if (null != code && code != StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode()) {
            return result;
        }
        if ("com.yxcorp.gifshow.detail.PhotoDetailActivity".equals(activity)) {
            WebElement element = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/back_btn");
            element.click();
            ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode());
            return result;
        }
        if ("com.yxcorp.login.userlogin.ResetSelectedAccountPasswordActivity".equals(activity)) {
            WebElement confirmBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/confirm_btn");
            WebElement loginPsdEt = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/login_psd_et");
            if (loginPsdEt != null) {
                loginPsdEt.sendKeys("a123456");
                Thread.sleep(5000);
                if (confirmBtn != null) {
                    confirmBtn.click();
                }
            }
            ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode());
            return result;
        }
        //首页
        if ("com.yxcorp.gifshow.HomeActivity".equals(activity)) {
            WebElement loginText = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/login_text");
            if (loginText != null) {
                loginText.click();
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode());
                return result;
            }
            WebElement leftText = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/left_text");
            if (leftText != null) {
                leftText.click();
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode());
                return result;
            }
            WebElement phoneLoginBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/phone_login_view");
            if (phoneLoginBtn != null) {
                phoneLoginBtn.click();
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode());
                return result;
            }
            WebElement closeBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/close_btn");
            if (closeBtn != null) {
                closeBtn.click();
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode());
                return result;
            }
            WebElement leftBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/left_btn");
            if (leftBtn == null) {
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_ELEMENT_IS_NULL.getCode());
                log.error("元素获取异常==>activity:{},id:{}", activity, "com.smile.gifmaker:id/left_btn");
                return result;
            }
            Thread.sleep(2000);
            leftBtn.click();
            Thread.sleep(2000);
            //查询左侧是否存在头像
            WebElement tabAvatar = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/tab_avatar");
            if (tabAvatar != null) {
                tabAvatar.click();
            } else {
                //不存在头像
                Thread.sleep(3000);
                tapPoint(androidDriver, 650, 1230);
                Thread.sleep(3000);
                // 点击我的
                tapPoint(androidDriver, 650, 1230);
                Thread.sleep(3000);
                tapPoint(androidDriver, 650, 400);
            }
            ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode());
            return result;
        }
        //大屏模式
        if ("com.yxcorp.gifshow.activity.BrowseSettingsActivity".equals(activity)) {
            //com.smile.gifmaker:id/browse_setting_new_btn 开启大屏模式  关闭大屏模式
            //com.smile.gifmaker:id/left_btn 返回
        }
        //登录
        if ("com.yxcorp.login.userlogin.LoginActivity".equals(activity)) {
            WebElement loginTitleTv = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/login_title_tv");
            WebElement loginNameEt = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/login_name_et");
            WebElement nextView = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/next_view");
            if (loginTitleTv != null) {
                if ("输入密码".equals(loginTitleTv.getText())) {
                    WebElement forgetPsdBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/forget_psd_btn");
                    if (forgetPsdBtn != null) {
                        forgetPsdBtn.click();
                    }
                }
            }
            if (loginNameEt != null) {
                String token = KuaimiUtil.kuaimiLogin();
                String mobile = KuaimiUtil.getMobile(token, "2307");
                result.put("token", token);
                result.put("mobile", mobile);
                loginNameEt.sendKeys(mobile);
                Thread.sleep(5000);
                nextView.click();
            }
            ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode());
            return result;
        }
        //重置密码
        if ("com.yxcorp.login.userlogin.RetrievePasswordActivity".equals(activity)) {
            String token = (String) result.get("token");
            String mobile = (String) result.get("mobile");
            WebElement nameEt = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/name_et");
            if (nameEt != null) {
                Thread.sleep(5000);
                WebElement verifyTv = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/verify_tv");
                if (verifyTv != null) {
                    verifyTv.click();
                    String msg = getMobileCaptcha(token, mobile);
                    if (msg != null) {
                        WebElement verifyEt = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/verify_et");
                        if (verifyEt != null) {
                            verifyEt.sendKeys(msg);
                            Thread.sleep(5000);
                            WebElement confirmBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/confirm_btn");
                            if (confirmBtn != null) {
                                confirmBtn.click();
                            }
                        }
                    }
                }
            }
            ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode());
            return result;
        }
        //验证码登录
        if ("com.yxcorp.login.userlogin.CaptchaCodeLoginActivity".equals(activity)) {
            String token = (String) result.get("token");
            String mobile = (String) result.get("mobile");
            WebElement captchaEt = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/captcha_et");
            if (captchaEt == null) {
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_ELEMENT_IS_NULL.getCode());
                log.error("元素获取异常==>activity:{},id:{}", activity, "com.smile.gifmaker:id/captcha_et");
                return result;
            }
            String captcha = getMobileCaptcha(token, mobile);
            if (captcha == null) {
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_MOBILE_CODE_IS_NULL.getCode());
                log.error("短信验证码获取异常==>activity:{}", activity);
                return result;
            }
            captchaEt.sendKeys(captcha);
            Thread.sleep(5000);
            WebElement captchaFinish = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/captcha_finish");
            if (captchaFinish == null) {
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_ELEMENT_IS_NULL.getCode());
                log.error("元素获取异常==>activity:{},id:{}", activity, "com.smile.gifmaker:id/captcha_finish");
                return result;
            }
            captchaFinish.click();
            ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode());
            return result;
        }
        //登录v2
        if ("com.yxcorp.login.userlogin.PhoneLoginV2Activity".equals(activity)) {
            String token = (String) result.get("token");
            String mobile = (String) result.get("mobile");
            WebElement phoneEt = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/phone_et");
            if (phoneEt == null) {
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_ELEMENT_IS_NULL.getCode());
                log.error("元素获取异常==>activity:{},id:{}", activity, "com.smile.gifmaker:id/fetch_code_tv");
                return result;
            }
            token = KuaimiUtil.kuaimiLogin();
            mobile = KuaimiUtil.getMobile(token, "2307");
            result.put("token", token);
            result.put("mobile", mobile);
            phoneEt.sendKeys(mobile);
            Thread.sleep(5000);
            WebElement fetchCodeTv = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/fetch_code_tv");
            if (fetchCodeTv == null) {
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_ELEMENT_IS_NULL.getCode());
                log.error("元素获取异常==>activity:{},id:{}", activity, "com.smile.gifmaker:id/fetch_code_tv");
                return result;
            }
            fetchCodeTv.click();
            String msg = getMobileCaptcha(token, mobile);
            if (msg == null) {
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_MOBILE_CODE_IS_NULL.getCode());
                log.error("短信验证码获取异常==>activity:{}", activity);
                return result;
            }
            WebElement captchaCodeEt = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/captcha_code_et");
            if (captchaCodeEt == null) {
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_ELEMENT_IS_NULL.getCode());
                log.error("元素获取异常==>activity:{},id:{}", activity, "com.smile.gifmaker:id/captcha_code_et");
                return result;
            }
            captchaCodeEt.sendKeys(msg);
            Thread.sleep(5000);
            WebElement cfmBtn = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/confirm_btn");
            if (cfmBtn == null) {
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_ELEMENT_IS_NULL.getCode());
                log.error("元素获取异常==>activity:{},id:{}", activity, "com.smile.gifmaker:id/confirm_btn");
                return result;
            }
            cfmBtn.click();
            ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode());
            return result;
        }
        ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_SUCCESS.getCode());
        return result;
    }

    private String getMobileCaptcha(String token, String mobile) throws Exception {
        String msg = null;
        int i = 0;
        while (i < 20) {
            i++;
            if (msg != null) {
                continue;
            }
            try {
                msg = KuaimiUtil.getMessage(token, "2307", mobile);
            } catch (Exception e) {
                log.info("等待{}短信验证码", mobile);
                Thread.sleep(5000);
            }
        }
        return msg;
    }


    public WebElement getElementByType(AndroidDriver androidDriver, String type, String typeInfo) {
        WebElement element = null;
        try {
            if (type.equals(FIND_ELEMENT_TYPE_ID)) {
                element = androidDriver.findElementById(typeInfo);
            } else if (type.equals(FIND_ELEMENT_TYPE_CLASS)) {
                element = androidDriver.findElementByClassName(typeInfo);
            } else if (type.equals(FIND_ELEMENT_TYPE_XPATH)) {
                element = androidDriver.findElementByXPath(typeInfo);
            }
        } catch (Exception e) {
            log.info("元素不存在===>type:{};typeInfo:{}", type, typeInfo);
        }
        return element;
    }

    public void checkActivity(AndroidDriver androidDriver) {
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String packageName = androidDriver.getCurrentPackage();
        String activity = androidDriver.currentActivity();
        if (null == packageName || !"com.smile.gifmaker".equals(packageName)) {
            log.error("app启动异常");
            return;
        }

        if (null != activity && "com.yxcorp.gifshow.webview.KwaiWebViewActivity".equals(activity)) {
            //激活账号页面
            WebElement backButton = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/left_tv");
            if (null != backButton) {
                backButton.click();
            }
            //悬浮层？？我知道了
            WebElement positive = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/positive");
            if (positive != null) {
                positive.click();
            }
        }
        //大屏模式悬浮层
        WebElement negativeElement = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/negative");
        if (null != negativeElement) {
            negativeElement.click();
        }
    }

    public void backToMain(AndroidDriver androidDriver) {
        WebElement element = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/back_btn");
        if (null != element) {
            element.click();
        }
    }

    public Boolean checkAdvertiser(AndroidDriver androidDriver, String advertiserName) {
        WebElement label = getElementByType(androidDriver, FIND_ELEMENT_TYPE_ID, "com.smile.gifmaker:id/label");
        String text = label.getText();
        if (null != label && null != text && text.startsWith(advertiserName)) {
            //表示广告筛选成功
            return true;
        } else {
            backToMain(androidDriver);
            return false;
        }
    }


    private void swipeByType(AndroidDriver androidDriver, String type) {
        if (type.equals(APP_SWIPE_TYPE_UP)) {
            this.swipeToUp(androidDriver);
        } else if (type.equals(APP_SWIPE_TYPE_DOWN)) {
            this.swipeToDown(androidDriver);
        } else if (type.equals(APP_SWIPE_TYPE_LEFT)) {
            this.swipeToLeft(androidDriver);
        } else if (type.equals(APP_SWIPE_TYPE_RIGHT)) {
            this.swipeToRight(androidDriver);
        }
    }

    private List<WebElement> getElementsByType(AndroidDriver androidDriver, String findType, String findKey) {
        List<WebElement> elements = null;
        if (findType.equals(FIND_ELEMENT_TYPE_ID)) {
            elements = androidDriver.findElementsById(findKey);
        } else if (findType.equals(FIND_ELEMENT_TYPE_CLASS)) {
            elements = androidDriver.findElementsByClassName(findKey);
        } else if (findKey.equals(FIND_ELEMENT_TYPE_XPATH)) {
            elements = androidDriver.findElementsByXPath(findKey);
        }
        return elements;
    }
}
