package com.yutou.qqbot.models.WebSign;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.Annotations.UseModel;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.utlis.*;
import com.yutou.napcat.event.MessageEvent;
import org.openqa.selenium.*;

import java.io.File;
import java.util.concurrent.TimeUnit;
@UseModel
public class Tsdm extends Model {
    @Override
    public boolean isUserPublic() {
        return false;
    }

    @Override
    public String[] getUsePowers() {
        return new String[]{
                QQFromCommands.TSDM_PAY
        };
    }

    @Override
    public String getModelName() {
        return "天使动漫签到";
    }

    @Override
    public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
        super.onMessage(qq, event, isGroup);
        if (msg.startsWith(QQFromCommands.TSDM_PAY)) {
            String url = msg.replace(QQFromCommands.TSDM_PAY, "").trim();
            if (StringUtils.isEmpty(url)) {
                QQBotManager.getInstance().sendMessage(qq, "使用说明：!tsdm [url]");
                return;
            }
            QQBotManager.getInstance().sendMessage(qq, "正在获取，请稍等...");
            QQBotManager.getInstance().sendMessage(qq, tsdm(url));
        }
    }

    @Override
    public void onTime(Long qq, String time) {
        super.onTime(qq, time);
        if ("08:05:00".equals(time)) {
            Log.i("开始天使动漫签到");
            try {
                if (tsdmSign()) {
                    QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "已完成天使动漫的签到");
                } else {
                    QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "天使动漫签到失败");
                }
            } catch (Exception e) {
                QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "天使动漫签到失败:" + AppTools.getExceptionString(e));
                e.printStackTrace();
            }finally {
                WebClient.getInstance().quit();
            }
        }
    }

    public boolean tsdmSign() {
        JSONArray array = JSON.parseArray(ConfigTools.readFile(new File("cookie.json")));
        if (array == null) {
            System.err.println("array is null");
            return false;
        }
        WebDriver driver = WebClient.getInstance().getWebDriver();
        try {
            driver.get("https://www.tsdm39.net/forum.php");
            getTsdm(array, driver);
            boolean isSign = false;
            for (WebElement element : driver.findElements(By.xpath("//a"))) {
                if (element.getText().contains("签到领奖!")) {
                    isSign = true;
                    break;
                }
            }
            if (!isSign) {
                return true;
            }
            try {
                ((JavascriptExecutor) driver).executeScript("showWindow('dsu_paulsign', 'plugin.php?id=dsu_paulsign:sign&9b5e8da2')");
            } catch (Exception ignored) {
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                driver.findElement(By.id("wl")).click();
                driver.findElement(By.xpath("//input[@value='3']")).click();
                driver.findElement(By.id("qiandao")).submit();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private void getTsdm(JSONArray array, WebDriver driver) {
        driver.manage().timeouts().implicitlyWait(10000, TimeUnit.SECONDS);
        driver.manage().deleteAllCookies();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (Cookie cookie : WebClient.loadCookie(array)) {
            driver.manage().addCookie(cookie);
        }
        driver.navigate().refresh();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public String tsdm(String url) {
        JSONArray array = JSON.parseArray(ConfigTools.readFile(new File("cookie.json")));
        if (array == null) {
            System.err.println("array is null");
            return null;
        }
        WebDriver driver = WebClient.getInstance().getWebDriver();
        driver.get(url);
        getTsdm(array, driver);
        try {
            ((JavascriptExecutor) driver).executeScript("document.getElementsByClassName('y viewpay')[0].click()");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            driver.findElement(By.id("payform")).submit();
        } catch (Exception ignored) {

        }
        String body = driver.findElement(By.name("tsdmlz")).getText();
        driver.close();
        driver.quit();
        return body;
    }

    public static void main(String[] args) {
        new Tsdm().tsdmSign();
    }
}
