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.Cookie;
import org.openqa.selenium.WebDriver;

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

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

    @Override
    public String getModelName() {
        return "NicePT签到";
    }

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

    public boolean sign() {
        String url = "https://www.nicept.net/attendance.php";
        JSONArray array = JSON.parseArray(ConfigTools.readFile(new File("nicept.json")));
        if (array == null) {
            System.err.println("array is null");
            return false;
        }
        WebDriver driver = WebClient.getInstance().getWebDriver();
        try {
            driver.manage().timeouts().implicitlyWait(10000, TimeUnit.SECONDS);
            driver.get(url);
            driver.navigate().refresh();
            for (Cookie _cookie : WebClient.loadCookie(array)) {
                driver.manage().addCookie(_cookie);
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            driver.get(url);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
