package com.xty.network.model

import com.chad.library.adapter.base.entity.MultiItemEntity
import java.io.Serializable
import java.text.SimpleDateFormat
import java.util.Calendar

data class ReportBean(
    val adorn: Adorn,//佩戴信息
    val status: String?,
    val star: String,//星级评分
    val healthy: Healthy,//健康信息
    val user: User,//用户信息
    val analyse: Analyse, //综合分析
    var analysis: Analysis,  //中西医分析
    var organ: Organ, //器官
    var sleep: SleepBean,  //直接使用睡眠的model
    var healthTotal: List<totalBean>,  //健康统计
    var healthTrend: List<ChildStatus>,   //健康趋势
    var statistical: List<StatisTical>? = null, //统计测量
    var signData: SingData,//基础信息
    var notifyMsg: String,//预警消息
    override var itemType: Int
) : MultiItemEntity, Serializable {
    data class SingData(
        var temp: HrInfo,//体温
        var dbp: HrInfo,//血压
        var hr: HrInfo,//心率
        var so: HrInfo,//血氧
        var uricAcid: UricAcid,//尿酸
        var step: HrInfo,//步数
        var stepData: HrInfo,//步数
        var respiratory: HrInfo,//呼吸率
        var bloodSugar: HrInfo,//血糖
        var sleep: HrInfo,//睡眠
        var bloodFat: BloodFatInfo //血脂
    ) : Serializable

    data class UricAcid(
        val min: String,
        val max: String,
        val avg: String,
        val count: String
    ) : Serializable

    data class HrInfo(
        var name: String, //名字
        var avg: String,  //平均
        var min: String,//最小
        var max: String,//最大
        var count: String,//次数
        var step: String,//步数
        var srcId: Int,//图片ID
        var avgLight: String? = null,//平均浅睡
        var maxTime: String? = null,//最大睡眠时间
        var avgTime: String? = null,//平均睡眠时间
        var minTime: String? = null,//最少睡眠时间
        var avgDeep: String? = null,//平均深睡
        var sleepDuration: Int? = null//(周，月，年，近7天，近30天)睡眠时长
    ) : Serializable

    data class BloodFatInfo(
        val ldlMin: Float,
        val ldlAvg: String,
        val tgMax: Float,
        val tcMax: Float,
        val count: Int,
        val hdlMin: Float,
        val hdlAvg: String,
        val tgMin: Float,
        val tgAvg: String,
        val ldlMax: Float,
        val tcMin: Float,
        val tcAvg: String,
        val hdlMax: Float

    ) : Serializable

    data class totalBean(
        var result: Int,
        var count: Int,
    ) : Serializable

    data class Organ(
        var kidney: OrganChild,//肾脏
        var kidneyListData: List<ChildStatus>,
        var lymph: OrganChild, //淋巴
        var lymphListData: List<ChildStatus>,
        var pericardium: OrganChild, //心包
        var pericardiumListData: List<ChildStatus>,
        var cholecyst: OrganChild, //胆囊
        var cholecystListData: List<ChildStatus>,
        var liver: OrganChild, //肝脏
        var liverListData: List<ChildStatus>,
        var spleen: OrganChild,//脾脏
        var spleenListData: List<ChildStatus>,
        var largeIntestine: OrganChild, //大肠
        var largeIntestineListData: List<ChildStatus>,
        var bladder: OrganChild, //膀胱
        var bladderListData: List<ChildStatus>,
        var lungs: OrganChild, //肺
        var lungsListData: List<ChildStatus>,
        var stomach: OrganChild, //胃
        var stomachListData: List<ChildStatus>,
        var smallIntestine: OrganChild, //小肠
        var smallIntestineListData: List<ChildStatus>,
        var heart: OrganChild, //心脏
        var heartListData: List<ChildStatus>,
    ) : Serializable

    data class OrganChild(
        var model: ChildStatus,
        var value: List<ChildStatus>,
    ) : Serializable

    data class Analysis(
        var sleep: List<Content>? = null,  //睡眠方案建议
        var WmAnalysis: List<Content>? = null,//西医分析
        var nutrition: List<Content>? = null,//营养处方建议
        var TcmAnalysis: List<Content>? = null, //中医分析
        var wm: List<Content>? = null,//西医指导
        var exercise: List<Content>? = null,//运动指导
        var tcm: List<Content>? = null,//中医养生指导
        var diet: List<Content>? = null,//饮食指导
    ) : Serializable

    data class Healthy(
        var healthyId: String,
        var userId: String,
        var createTime: String,
        var result: Int,// -1 建模中 0数据不足 1健康 2亚健康 3疾病
    ) : Serializable

    data class Content(

        var name: String,
        var dictName: String,
        var content: String
    ) : Serializable

    data class Analyse(
        val sleep: String,
        val temp: String,
        val dbp: String,
        val exercise: String,
        val exerciseStep: String,
        val so: String,
        val respiratory: String,
        val status: String,//-1 建模中 0数据不足 1健康 2亚健康 3疾病
        val hr: String,
        val bloodsugar: String,
        val uricAcid: String,//尿酸
        val tc: String,//总胆固醇
        val tg: String,//甘油三酯
        val hdl: String,//高密度脂蛋白
        val ldl: String//低密度脂蛋白
    ) : Serializable

    data class Adorn(
        val activeWear: Int,
        val dormancy: Int,
        val wearCount: Int
    ) : Serializable

    data class User(
        val actCity: Any,
        val actDistrict: Any,
        val actProvince: Any,
        val area: String,
        val age: String,
        val avatarUrl: String,
        val birthday: String,
        val city: String,
        val complexion: Any,
        val createBy: String,
        val createTime: Any,
        val deviceId: String,
        val disease: Any,
        val district: String,
        val emergencyContact: Any,
        val esignature: String,
        val headPortrait: Any,
        val healthy: Any,
        val height: Double,
        val hips: Any,
        val historyId: String,
        val id: Int,
        val idCard: String,
        val latitude: String,
        val longitude: String,
        val medicalHistory: String,
        val name: String,
        val openId: Any,
        val pageNum: Any,
        val pageSize: Any,
        val password: String,
        val phone: String,
        val province: String,
        val remark: Any,
        val sPeriodic: Any,
        val sex: String,//1男 2女
        val speriodic: Any,
        val updateBy: String,
        val updateTime: Any,
        val urgent: String,
        val urgent1: Any,
        val urgentPhone: String,
        val urgentPhone1: Any,
        val urgentRelation: String,
        val urgentRelation1: Any,
        val userCode: Any,
        val userId: Int,
        val waistline: Any,
        val weight: Double,
        var other: String,
        var historyName: String,
        var tumour: String,
        var diseaseList: MutableList<Operation>
    ) : Serializable

    data class Operation(
        var name: String,
        var time: String,
        var type: Int
    ) : Serializable

    data class StatisTical(
        var start_time: Long,
        var min: Int,
        var max: Int,
        var maxStartTime: Long,
        var minStartTime: Long,
        var count: Int,
        var validCount: Int,
    ) : Serializable

    data class ChildStatus(
        var id: Int,
        var idHealth: Int,//0数据不足，1健康，2亚健康，3疾病
        var result: Int,
        var sumNoEnough: Int,//-1，建模中，>=0,完成了经络分析
        var userId: Int,
        var createTime: String,
    ) : Serializable {
        constructor() : this(0, 0, 0, 0, 0, "")
    }

    //演示模式逻辑
    fun fillDemoChildStatusList(year: String, healthTrend: List<ChildStatus>): List<ChildStatus> {

        val filledDataList = mutableListOf<ChildStatus>()
        // 循环遍历12个月
        for (month in 1..12) {
            val formattedMonth = monthToDateString("2023", month)
            // 查找数据集合中是否存在当前月份的数据
            val matchingData =
                healthTrend.find {
                    modifyToFirstDayOfMonth(it.createTime) == formattedMonth
                }

            // 如果存在匹配的数据，则添加到补全后的列表中
            if (matchingData != null) {
                matchingData.createTime = formattedMonth
                filledDataList.add(matchingData)
            } else {
                // 不存在匹配的数据，则添加一个值为0的数据
                filledDataList.add(ChildStatus(0, 0, 0, 0, 0, formattedMonth))
            }
        }
        return filledDataList
    }

    fun fillChildStatusList(year: String, healthTrend: List<ChildStatus>): List<ChildStatus> {
        if (healthTrend.size == 12) {
            return healthTrend
        }
        val filledDataList = mutableListOf<ChildStatus>()

        // 如果是当年，则最大月份为当前月
        val currentYear = Calendar.getInstance().get(Calendar.YEAR)
        val currentMonth =
            Calendar.getInstance().get(Calendar.MONTH) + 1 // Calendar.MONTH is 0-based
        val totalMonth = if (currentYear == year.toInt()) currentMonth else 12
        // 循环遍历12个月
        for (month in 1..totalMonth) {
            val formattedMonth = monthToDateString(year, month)

            // 查找数据集合中是否存在当前月份的数据
            val matchingData =
                healthTrend.find {
                    modifyToFirstDayOfMonth(it.createTime) == formattedMonth
                }

            // 如果存在匹配的数据，则添加到补全后的列表中
            if (matchingData != null) {
                matchingData.createTime = formattedMonth
                filledDataList.add(matchingData)
            } else {
                // 不存在匹配的数据，则添加一个值为0的数据
                filledDataList.add(ChildStatus(0, 0, 0, 0, 0, formattedMonth))
            }
        }
        return filledDataList
    }

    private fun modifyToFirstDayOfMonth(inputDate: String): String {
        val dateFormatInput = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val dateFormatOutput = SimpleDateFormat("yyyy-MM-01 00:00:00")

        val date = dateFormatInput.parse(inputDate)

        val calendar = Calendar.getInstance()
        calendar.time = date

        return dateFormatOutput.format(calendar.time)
    }

    private fun monthToDateString(year: String, month: Int): String {
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR, year.toInt())
        calendar.set(Calendar.MONTH, month - 1)  // Month is 0-based in Calendar
        calendar.set(Calendar.DAY_OF_MONTH, 1)   // Set day to 1st day of the month
        calendar.set(Calendar.HOUR_OF_DAY, 0)    // Set hour to midnight
        calendar.set(Calendar.MINUTE, 0)         // Set minute to 0
        calendar.set(Calendar.SECOND, 0)         // Set second to 0

        val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        return dateFormat.format(calendar.time)
    }
}


