package com.yutou.qqbot.models.WebSign;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.yutou.qqbot.Annotations.UseModel;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.utlis.ConfigTools;
import com.yutou.qqbot.utlis.StringUtils;
import com.yutou.qqbot.utlis.WebClient;
import org.openqa.selenium.*;

import java.io.File;

@UseModel
public class ZhuZhu extends Model {
    @Override
    public boolean isUserPublic() {
        return false;
    }

    @Override
    public String[] getUsePowers() {
        return new String[0];
    }

    @Override
    public String getModelName() {
        return "HiRes猪猪论坛签到模块";
    }

    @Override
    public synchronized void onTime(Long qq, String time) {
        super.onTime(qq, time);
        if ("09:01:00".equals(time)) {
            if(sign()){
                QQBotManager.getInstance().sendMessage("HiRes论坛签到成功");
            }else{
                QQBotManager.getInstance().sendMessage("HiRes论坛签到失败");
            }
            WebClient.getInstance().quit();
        }
    }

    public boolean sign() {
        String url = "https://magpapa.online/";
        String cookie = ConfigTools.readFile(new File("zhuzhu.cookie"));
        if (StringUtils.isEmpty(cookie)) {
            return false;
        }
        JSONArray array = JSON.parseArray(cookie);
        WebDriver driver = WebClient.getInstance().getWebDriver();
        try {
            driver.get(url);
            driver.manage().deleteAllCookies();
            for (Cookie _cookie : WebClient.loadCookie(array)) {
                driver.manage().addCookie(_cookie);
                Thread.sleep(100);
            }
            driver.navigate().refresh();
            driver.get("https://magpapa.online/plugin.php?id=dc_signin");
            for (WebElement element : driver.findElements(By.xpath("//a"))) {
                if ("签到".equals(element.getText())) {
                    element.click();
                    Thread.sleep(1000);
                    click(driver);
                    Thread.sleep(1000);
                    break;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    private void click(WebDriver driver){
        driver.findElement(By.className("dcsignin_list")).findElement(By.id("emot_9")).click();
        driver.findElement(By.id("fwin_content_sign")).findElement(By.name("signpn")).click();
    }
}
