# -*- coding: utf-8 -*-
from __future__ import annotations
import sys, os, io, csv, time, datetime
from typing import Callable, Optional

# PyQt5
from PyQt5 import QtWidgets, uic
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QTimer, QSize
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import (
    QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
    QLabel, QPushButton, QComboBox, QTextEdit, QFileDialog, QMessageBox,
    QGroupBox, QGridLayout, QDialog, QFormLayout, QSpinBox, QCheckBox,
    QLineEdit, QTableWidget, QTableWidgetItem
)
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import cv2
from PIL import Image
import serial
import serial.tools.list_ports

# ---------- 协议导入----------
from Shared_CODE.FaceRecognitionProtocol import (
    build_reset, build_uvc_view, build_face_view, build_verify,
    build_enroll_itg_single, build_delete_all, build_get_all_userid,build_delete_user,
    MID_REPLY, MID_NOTE, CMD_ENROLL, CMD_ENROLL_ITG,
    parse_reply, parse_note
)

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
ui_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../Shared_UI'))
users_ui_file_path = os.path.join(ui_path, "users.ui")
verify_ui_file_path = os.path.join(ui_path, "verify.ui")
enrill_ui_file_path = os.path.join(ui_path, "enroll.ui")

CSV_FILE = os.path.join(ui_path, "users.csv")
# -------------------- CSV 工具 --------------------"

def load_users():
        users = []
        try:
            with open(CSV_FILE, "r", encoding="utf-8") as f:
                reader = csv.reader(f)
                for row in reader:
                    if len(row) >= 2:
                        user = {"user_id": row[0], "user_name": row[1]}
                        user["created_at"] = row[2] if len(row) >= 3 else ""
                        users.append(user)
        except FileNotFoundError:
            pass
        return users


def save_users_list(users):
        with open(CSV_FILE, "w", newline="", encoding="utf-8") as f:
            w = csv.writer(f)
            for u in users:
                w.writerow([u.get("user_id", ""), u.get("user_name", ""), u.get("created_at", "")])


def save_user(user_id: int, user_name: str) -> bool:
        users = load_users()
        for u in users:
            if str(u["user_id"]) == str(user_id):
                return False
        created_at = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        users.append({"user_id": str(user_id), "user_name": user_name, "created_at": created_at})
        save_users_list(users)
        return True

class UserManageDialog(QDialog):
    def __init__(self, parent, send_func):
        super().__init__(parent)
        uic.loadUi(users_ui_file_path, self)
        self.send_func = send_func
        self.btn_delete.clicked.connect(self.delete_selected)
        self.btn_refresh.clicked.connect(self.refresh)
        self.btn_get.clicked.connect(self.get_from_device)
        self.btn_del_all.clicked.connect(self.delete_all_users)
        self.refresh()


    def refresh(self):
        users = load_users()
        self.table.setRowCount(len(users))
        for r, u in enumerate(users):
            self.table.setItem(r, 0, QTableWidgetItem(str(u.get("user_id", ""))))
            self.table.setItem(r, 1, QTableWidgetItem(u.get("user_name", "")))
            self.table.setItem(r, 2, QTableWidgetItem(u.get("created_at", "")))

    def delete_selected(self):
        row = self.table.currentRow()
        if row < 0:
            QMessageBox.warning(self, "提示", "请选择要删除的用户")
            return
        uid = self.table.item(row, 0).text()
        uname = self.table.item(row, 1).text()
        try:
            uid_int = int(uid)
            self.send_func(build_delete_user(uid_int))
        except:
            pass
        users = [u for u in load_users() if str(u["user_id"]) != uid]
        save_users_list(users)
        self.refresh()
        QMessageBox.information(self, "提示", f"用户 {uname}(ID={uid}) 已删除")

    def get_from_device(self):
        self.send_func(build_get_all_userid())

    def delete_all_users(self):
        self.send_func(build_delete_all())
        save_users_list([])
        self.refresh()
        QMessageBox.information(self, "提示", "已请求删除所有用户并清空本地记录")

class VerifyDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        uic.loadUi(verify_ui_file_path, self)
        self.cb_rightaway.setCurrentIndex(0)
        self.sb_timeout.setValue(10)
        self.btn_ok.clicked.connect(self.accept)

    def values(self):
        pd_val = self.cb_rightaway.currentIndex()
        timeout_val = self.sb_timeout.value()
        return pd_val, timeout_val


class EnrollItgSingleDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        uic.loadUi(enrill_ui_file_path, self)
        self.cb_admin.setCurrentIndex(0)
        self.btn_ok.clicked.connect(self.accept)

    def values(self):
        admin_val = self.cb_admin.currentIndex()
        uname = self.le_name.text().strip()[:32]
        face_dir = 0
        if self.chk_mid.isChecked(): face_dir |= 0x01
        if self.chk_right.isChecked(): face_dir |= 0x02
        if self.chk_left.isChecked(): face_dir |= 0x04
        if self.chk_down.isChecked(): face_dir |= 0x08
        if self.chk_up.isChecked(): face_dir |= 0x10
        if face_dir == 0: face_dir = 0x01
        timeout_val = self.sb_timeout.value()
        try:
            itg_val = int(self.le_itg.text().strip(), 0)
        except ValueError:
            itg_val = 0
        return admin_val, uname, face_dir, timeout_val, itg_val

if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = UserManageDialog()
    dialog.exec()
    sys.exit(0)
