/*
 * 自然写互动课堂应用开发SDK软件 V1.0
 * PenDevice - 点阵笔设备数据模型
 *
 * 描述：封装点阵笔的设备信息、连接状态、能力参数等
 */

package com.writech.sdk.model;

import java.io.Serializable;

/**
 * 点阵笔设备模型
 * 包含设备基本信息、硬件参数和连接状态
 */
public class PenDevice implements Serializable {

    private static final long serialVersionUID = 1L;

    /* ========== 基本信息 ========== */

    /** 设备MAC地址（唯一标识） */
    private String macAddress;

    /** 设备名称（用户可自定义） */
    private String deviceName;

    /** 设备型号（如 WP-200, WP-300） */
    private String modelName;

    /** 固件版本号（如 2.1.5） */
    private String firmwareVersion;

    /** 硬件版本号 */
    private String hardwareVersion;

    /** 设备序列号 */
    private String serialNumber;

    /* ========== 硬件能力 ========== */

    /** 采样率（Hz，常见值：100, 200） */
    private int sampleRate;

    /** 压力感应级别（常见值：1024, 2048, 4096） */
    private int pressureLevels;

    /** 坐标分辨率（DPI，常见值：300, 600） */
    private int coordinateDpi;

    /** 是否支持倾斜角检测 */
    private boolean tiltSupported;

    /** BLE协议版本（4.2 / 5.0 / 5.3） */
    private String bleVersion;

    /** 电池容量（mAh） */
    private int batteryCapacity;

    /* ========== 运行状态 ========== */

    /** 连接状态枚举 */
    public enum ConnectionState {
        DISCONNECTED,       /* 未连接 */
        CONNECTING,         /* 正在连接 */
        CONNECTED,          /* 已连接 */
        RECONNECTING        /* 正在重连 */
    }

    /** 当前连接状态 */
    private ConnectionState connectionState = ConnectionState.DISCONNECTED;

    /** 当前电量百分比（0-100） */
    private int batteryLevel;

    /** 是否正在充电 */
    private boolean isCharging;

    /** 是否正在书写（笔尖接触纸面） */
    private boolean isWriting;

    /** 信号强度RSSI（dBm） */
    private int rssi;

    /** 最后一次通信时间（毫秒时间戳） */
    private long lastCommunicationTime;

    /** 累计书写时长（秒） */
    private long totalWritingDuration;

    /** 绑定的学生ID */
    private String boundStudentId;

    /** 绑定的学生姓名 */
    private String boundStudentName;

    /* ========== 构造函数 ========== */

    public PenDevice() {
    }

    public PenDevice(String macAddress, String deviceName) {
        this.macAddress = macAddress;
        this.deviceName = deviceName;
        this.sampleRate = 100;
        this.pressureLevels = 4096;
        this.coordinateDpi = 300;
    }

    /* ========== Getter / Setter ========== */

    public String getMacAddress() { return macAddress; }
    public void setMacAddress(String macAddress) { this.macAddress = macAddress; }

    public String getDeviceName() { return deviceName; }
    public void setDeviceName(String deviceName) { this.deviceName = deviceName; }

    public String getModelName() { return modelName; }
    public void setModelName(String modelName) { this.modelName = modelName; }

    public String getFirmwareVersion() { return firmwareVersion; }
    public void setFirmwareVersion(String firmwareVersion) { this.firmwareVersion = firmwareVersion; }

    public String getHardwareVersion() { return hardwareVersion; }
    public void setHardwareVersion(String v) { this.hardwareVersion = v; }

    public String getSerialNumber() { return serialNumber; }
    public void setSerialNumber(String serialNumber) { this.serialNumber = serialNumber; }

    public int getSampleRate() { return sampleRate; }
    public void setSampleRate(int sampleRate) { this.sampleRate = sampleRate; }

    public int getPressureLevels() { return pressureLevels; }
    public void setPressureLevels(int pressureLevels) { this.pressureLevels = pressureLevels; }

    public int getCoordinateDpi() { return coordinateDpi; }
    public void setCoordinateDpi(int coordinateDpi) { this.coordinateDpi = coordinateDpi; }

    public boolean isTiltSupported() { return tiltSupported; }
    public void setTiltSupported(boolean tiltSupported) { this.tiltSupported = tiltSupported; }

    public String getBleVersion() { return bleVersion; }
    public void setBleVersion(String bleVersion) { this.bleVersion = bleVersion; }

    public int getBatteryCapacity() { return batteryCapacity; }
    public void setBatteryCapacity(int batteryCapacity) { this.batteryCapacity = batteryCapacity; }

    public ConnectionState getConnectionState() { return connectionState; }
    public void setConnectionState(ConnectionState state) { this.connectionState = state; }

    public int getBatteryLevel() { return batteryLevel; }
    public void setBatteryLevel(int batteryLevel) { this.batteryLevel = batteryLevel; }

    public boolean isCharging() { return isCharging; }
    public void setCharging(boolean charging) { isCharging = charging; }

    public boolean isWriting() { return isWriting; }
    public void setWriting(boolean writing) { isWriting = writing; }

    public int getRssi() { return rssi; }
    public void setRssi(int rssi) { this.rssi = rssi; }

    public long getLastCommunicationTime() { return lastCommunicationTime; }
    public void setLastCommunicationTime(long t) { this.lastCommunicationTime = t; }

    public long getTotalWritingDuration() { return totalWritingDuration; }
    public void setTotalWritingDuration(long d) { this.totalWritingDuration = d; }

    public String getBoundStudentId() { return boundStudentId; }
    public void setBoundStudentId(String id) { this.boundStudentId = id; }

    public String getBoundStudentName() { return boundStudentName; }
    public void setBoundStudentName(String name) { this.boundStudentName = name; }

    /* ========== 便捷方法 ========== */

    /** 是否已连接 */
    public boolean isConnected() {
        return connectionState == ConnectionState.CONNECTED;
    }

    /** 电量是否低（<= 10%） */
    public boolean isLowBattery() {
        return batteryLevel <= 10 && !isCharging;
    }

    /** 获取设备显示名称（优先显示学生姓名） */
    public String getDisplayName() {
        if (boundStudentName != null && !boundStudentName.isEmpty()) {
            return boundStudentName + "的笔";
        }
        return deviceName != null ? deviceName : "WritechPen-" + macAddress;
    }

    @Override
    public String toString() {
        return "PenDevice{" +
               "mac='" + macAddress + '\'' +
               ", name='" + deviceName + '\'' +
               ", model='" + modelName + '\'' +
               ", state=" + connectionState +
               ", battery=" + batteryLevel + "%" +
               ", writing=" + isWriting +
               '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PenDevice that = (PenDevice) o;
        return macAddress != null && macAddress.equals(that.macAddress);
    }

    @Override
    public int hashCode() {
        return macAddress != null ? macAddress.hashCode() : 0;
    }
}
