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.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.io.File;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
@UseModel
public class BaiHeHui extends Model {
    @Override
    public boolean isUserPublic() {
        return true;
    }

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

    @Override
    public String getModelName() {
        return "百合会签到";
    }

    @Override
    public void onTime(Long qq, String time) {
        super.onTime(qq, time);
        if ("08:10:00".equals(time)) {
            Log.i("开始百合会签到");
            try {
                if (sign()) {
                    QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "百合会签到成功");
                } else {
                    QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "百合会签到失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
                QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "百合会签到失败:" + AppTools.getExceptionString(e));
            } finally {
                WebClient.getInstance().quit();
            }
        }
    }

    public boolean sign() {
        WebDriver driver = WebClient.getInstance().getWebDriver();
        try {
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
            driver.get("https://bbs.yamibo.com/forum.php");
            login(driver);
            //  driver.manage().deleteAllCookies();
           /* Thread.sleep(1000);
            for (Cookie _cookie : WebClient.loadCookie(array)) {
                System.err.println(_cookie);
                driver.manage().addCookie(_cookie);
                Thread.sleep(100);
            }*/
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            driver.navigate().refresh();
            driver.get("https://bbs.yamibo.com/plugin.php?id=zqlj_sign");
            for (WebElement element : driver.findElements(By.xpath("//a"))) {
                if ("点击打卡".equals(element.getText())) {
                    element.click();
                    break;
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private void login(WebDriver driver) {
        driver.get("https://bbs.yamibo.com/member.php?mod=logging&action=login");
        driver.findElement(By.name("username")).sendKeys("z583819556");
        driver.findElement(By.name("password")).sendKeys("u8fi4Qfr.5D2wZ5");
        driver.findElement(By.name("loginsubmit")).submit();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        new BaiHeHui().sign();
    }
}
