package cn.com.ctop.crawler.modules.appium.service.impl;

import cn.com.ctop.crawler.modules.appium.entity.AppiumDevice;
import cn.com.ctop.crawler.modules.appium.entity.AppiumTask;
import cn.com.ctop.crawler.modules.appium.entity.AppiumTaskItem;
import cn.com.ctop.crawler.modules.appium.entity.AppiumTaskLog;
import cn.com.ctop.crawler.modules.appium.mapper.AppiumTaskLogMapper;
import cn.com.ctop.crawler.modules.appium.mapper.AppiumTaskMapper;
import cn.com.ctop.crawler.modules.appium.service.IAppiumDeviceService;
import cn.com.ctop.crawler.modules.appium.service.IAppiumTaskItemService;
import cn.com.ctop.crawler.modules.appium.service.IAppiumTaskService;
import cn.com.ctop.crawler.modules.core.util.AppiumUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.appium.java_client.android.AndroidDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 任务表
 * @author jeecg-boot
 * @date 2019-10-31
 * @version V1.0
 */
@Service
public class AppiumTaskServiceImpl extends ServiceImpl<AppiumTaskMapper, AppiumTask> implements IAppiumTaskService {
    @Value("${kuaishou.group-control.path}")
    private String groupControlPath;
    @Value("${kuaishou.group-control.port}")
    private String groupControlPort;
    @Autowired
    private IAppiumTaskItemService iAppiumTaskItemService;
    @Autowired
    private IAppiumDeviceService iAppiumDeviceService;
    @Autowired
    private AppiumTaskLogMapper appiumTaskLogMapper;

    @Override
    public void runKuaishouLoginTask() {
        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.gifshow.HomeActivity");
            appiumUtil.loginTask(androidDriver, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void runTask(Long taskId, Integer deviceId, String publishName) {
        //获取当前需要执行的任务信息
        AppiumTask task = this.getById(taskId);
        //获取手机设备信息
        AppiumDevice appiumDevice = iAppiumDeviceService.getById(deviceId);
        appiumDevice.setStatus(2);
        iAppiumDeviceService.updateById(appiumDevice);
        AppiumTaskLog log = new AppiumTaskLog();
        log.setDeviceId(deviceId);
        log.setDeviceIp(appiumDevice.getIp());
        log.setDevicePort(appiumDevice.getPort());
        log.setStatus(1 + "");
        log.setTaskId(taskId);
        log.setTaskName(task.getTaskName());
        appiumTaskLogMapper.insert(log);
        log.setId(log.getId());
        //查询所需运行的任务列表
        List<AppiumTaskItem> taskItemList = iAppiumTaskItemService.initTaskItemList(taskId, null);
        AppiumUtil appiumUtil = new AppiumUtil();
        try {
            AndroidDriver androidDriver = appiumUtil.initAndroidDriver(groupControlPath + ":" + groupControlPort + "/wd/hub", appiumDevice.getIp() + ":" + appiumDevice.getPort(), task.getAppPackage(), task.getAppActivity());
            if (taskItemList != null && taskItemList.size() > 0) {
                for (AppiumTaskItem appiumTaskItem : taskItemList) {
                    appiumUtil.runTask(androidDriver, appiumTaskItem, null, publishName);
                }
                log.setStatus(2 + "");
                appiumTaskLogMapper.updateById(log);
                appiumDevice.setStatus(1);
                iAppiumDeviceService.updateById(appiumDevice);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.setStatus(3 + "");
            appiumTaskLogMapper.updateById(log);
            appiumDevice.setStatus(1);
            iAppiumDeviceService.updateById(appiumDevice);
        }
    }
}
