package com.performancecoachlab.posedetection.skeleton


data class Pose(
    val leftShoulder: PoseRange? = null,
    val rightShoulder: PoseRange? = null,
    val leftElbow: PoseRange? = null,
    val rightElbow: PoseRange? = null,
    val leftHip: PoseRange? = null,
    val rightHip: PoseRange? = null,
    val leftKnee: PoseRange? = null,
    val rightKnee: PoseRange? = null,
) {
    data class PoseRange(
        val min: Double, val max: Double
    )

    fun matches(skeleton: Skeleton): Boolean = matches(skeleton.getAngles())

    private fun matches(skeletonAngles: Skeleton.SkeletonAngles): Boolean {
        return angleInRange(
            leftShoulder,
            skeletonAngles.leftShoulder
        ) && angleInRange(rightShoulder, skeletonAngles.rightShoulder) && angleInRange(
            leftElbow,
            skeletonAngles.leftElbow
        ) && angleInRange(rightElbow, skeletonAngles.rightElbow) && angleInRange(
            leftHip,
            skeletonAngles.leftHip
        ) && angleInRange(rightHip, skeletonAngles.rightHip) && angleInRange(
            leftKnee,
            skeletonAngles.leftKnee
        ) && angleInRange(rightKnee, skeletonAngles.rightKnee)

    }

    private fun angleInRange(angleInPose: PoseRange?, angleInSkeleton: Double?): Boolean {
        return angleInPose?.let { poseAngleRange ->
            angleInSkeleton?.let { skeletonAngle ->
                skeletonAngle >= poseAngleRange.min && skeletonAngle <= poseAngleRange.max
            } ?: false
        } ?: true
    }
}
