import base64
import json
import os.path
import random
import threading
import time

import requests
from loguru import logger
from sqlmodel import Session, select

from .base import BaseReporter
from ...config.config import AppCtx
from ...models.report_urls import ReportUrlModel
from ...utils.common import get_proxies, get_all_cookies, md5
from ...utils.gen_cookie import GenCookie
from ...utils.ua import random_ua


class WapReporter(BaseReporter):
    def __init__(self):

        self.engine_name = "WAP_REPORTER"
        self.status = 1
        self.ev = threading.Event()

        self.report_url = "https://ufosdk.baidu.com/api?m=Client&a=postMsg"
        self.request = requests.session()
        self.proxies = get_proxies()
        self.headers = {
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'User-Agent': random_ua(is_wap=True),
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,zh-TW;q=0.6',
            'sec-ch-ua_wap': '"Not(A:Brand";v="99", "Google Chrome";v="133", "Chromium";v="133"',
            'sec-ch-ua_wap-mobile': '?0',
            'sec-ch-ua_wap-platform': '"Windows"',
            "Cookie": "",
        }

        self.database = AppCtx.g_db_engine
        self.all_cookies = get_all_cookies()

    def stop(self):
        self.status = 0
        self.ev.set()
        logger.warning(f"{self.engine_name} 收到退出消息，等待当前任务完成后退出")

    def run(self):
        """实现 WAP 端的举报逻辑"""
        with Session(self.database) as session:
            stmt = select(ReportUrlModel).where(ReportUrlModel.is_report_by_wap == False).where(
                ReportUrlModel.has_evidence == True)
            rows: list[ReportUrlModel] = session.exec(stmt).all()

            logger.debug(f"[{self.engine_name}] 共找到 {len(rows)} 条待举报记录")

            for row in rows:

                if not self.status:
                    break

                self.ev.wait(1)

                # 选个 cookie
                report_cookie = random.choice(get_all_cookies())
                report_site_cookie = GenCookie.run(report_cookie)
                self.headers["Cookie"] = report_site_cookie
                self.headers["User-Agent"] = random_ua()
                logger.debug(f"{report_cookie=}")

                # 获取用户信息
                userinfo = self.get_user_info()
                if not userinfo:
                    logger.warning(f"[{self.engine_name}] 跳过 {row.surl} 的举报，userinfo 获取失败")
                    continue

                # 举报
                img_path = f"./imgs/{row.domain}/{md5(row.surl)}.png"
                if not os.path.exists(img_path):
                    logger.error(f"截图文件 {img_path} 不存在")
                    continue
                result = self.do_report(userinfo, img_path, row.surl, row.q)
                if result:
                    row.is_report_by_wap = True
                    session.add(row)
                    session.commit()

            self.ev.wait(5)

    def get_user_info(self):
        try:
            userinfo = {}
            # wapUserAgent = random.choice(self.wapUserAgent)
            response = self.request.get(
                "https://ufosdk.baidu.com/api?m=Web&a=getUserInfo&appid=293852",
                headers=self.headers, proxies=self.proxies, allow_redirects=False, timeout=10
            )
            json_data = response.json()
            logger.debug(f"{self.engine_name} get_user_info response: {json_data}")
            uid = json_data['result']['uid']
            un = json_data['result']['un']
            userinfo["uid"] = uid
            userinfo["un"] = un
            logger.debug(f"{userinfo=}")
            return userinfo
        except Exception as e:
            logger.error(f"[{self.engine_name}]获取用户信息错误: {e}")
            return None

    def do_report(self, userinfo: dict, img_path: str, fb_url: str, extend_query: str):

        # 组装 extra 数据
        with open(img_path, "rb") as fp:
            img_data = fp.read()
            img_data = base64.b64encode(img_data).decode("utf-8")

        extra_string_data = {
            "ufo_app_version": "3.0",
            "feedback_position": "0",
            "extend_query": extend_query,
            "resource_id": "1599",
            "feedback_source": "其他",
            "feedback_source_text": "其他",
            "extend_url": f"https://wap.baidu.com/s?word=url:{fb_url}",
            "fb_url": fb_url,
            "extend_feedback_channel": "36923",
            "relation_words": "",
            "industry_one": "30",
            "industry_two": "197",
            "user": userinfo["un"],
            "baiducuid": ""
        }

        post_data = {
            "appid": "293852",
            "content": f"{fb_url} 存在大量色情淫秽信息",
            "uid": userinfo["uid"],
            "uname": userinfo["un"],
            "ajax": "1",
            "submit_type": "1",
            "extend_feedback_channel": "0",
            "baiducuid": "",
            "extend_url": "",
            "extrastring": json.dumps(extra_string_data, ensure_ascii=False),
            "screenshot[]": f"data:image/png;base64,{img_data}"
        }

        response = self.request.post(
            self.report_url,
            data=post_data,
            headers=self.headers,
            proxies=self.proxies,
            allow_redirects=False,
            timeout=10,
        )
        # logger.debug(req.json())
        data = response.json()
        logger.debug(data)
        if data['errno'] == 0:
            logger.success(f"[{self.engine_name}] {fb_url} 举报成功")
            return True
        if "请勿重复提交" in data["errmsg"]:
            logger.success(f"[{self.engine_name}] {fb_url} 重复提交，标记为成功")
            return True
        return False
