export default function cal(coords) {
    //  72关键点
    const eyeRes = eyes(coords);
    const mouthRes = mouth(coords);
    const eyebrows = eyebrow(coords);
    console.log('睁左、睁右、翻上、翻下、斗左、斗右');
    console.log(eyeRes);
    console.log('张闭嘴、嘴角左、嘴角右');
    console.log(mouthRes);
    console.log('左眉毛高、右眉毛高');
    console.log(eyebrows);
    return [...eyeRes, ...mouthRes, ...eyebrows];
}

function eyes(data) {
    // 睁左、睁右、翻上、翻下、斗左、斗右
    let [openLeft, openRight, verticalLeft, verticalRight, horizontalLeft, horizontalRight] = [0, 0, 0, 0, 0, 0, 0];
    const [left1, right1, top1, bottom1, center1] = [data[13], data[17], data[15], data[19], data[21]];
    const [left2, right2, top2, bottom2, center2] = [data[30], data[34], data[32], data[36], data[38]];

    // 以眉毛高度的1/2为基准
    const h = Math.min(data[28][1] - data[24][1], data[45][1] - data[41][1]) * 0.5 || 0.1;

    // x\y距离
    const x1 = right1[0] - left1[0];
    const x2 = right2[0] - left2[0];
    const y1 = bottom1[1] - top1[1];
    const y2 = bottom2[1] - top2[1];

    /**
     * open
     */
    // 睁眼 left
    const refactor = 0.23;
    console.log((data[19][1] - data[15][1]) / (data[17][0] - data[13][0]))
    console.log((data[36][1] - data[32][1]) / (data[34][0] - data[30][0]))
    console.log('左眼睁眼幅度：', (data[19][1] - data[15][1]) / (data[17][0] - data[13][0]))
    if ((data[19][1] - data[15][1]) / (data[17][0] - data[13][0]) > refactor) {
        openLeft = 1;
    }

    // 睁眼 right
    console.log('右眼睁眼幅度：', (data[36][1] - data[32][1]) / (data[34][0] - data[30][0]))
    if ((data[36][1] - data[32][1]) / (data[34][0] - data[30][0]) > refactor) {
        openRight = 1;
    }

    /**
     * vertical
     */
    // vertical left
    console.log('左上:' + (center1[1] - top1[1]))
    console.log('左下:' + (data[19][1] - center1[1]))
    console.log('左比:' +  ((center1[1] - top1[1]) / (data[19][1] - center1[1])))
    console.log('右上:' + (center2[1] - top2[1]))
    console.log('右下:' + (data[36][1] - center2[1]))
    console.log('右比:' +  ((center2[1] - top2[1]) / (data[36][1] - center2[1])))


    if ( (center1[1] - top1[1]) / (data[19][1] - center1[1]) < 0.925) {
        verticalLeft = 1;
    }
    else if ((center1[1] - top1[1]) / (data[19][1] - center1[1]) > 0.96) {
        verticalLeft = 2;
    }
    // vertical right
    if ((center2[1] - top2[1]) / (data[36][1] - center2[1]) < 0.925) {
        verticalRight = 1;
    }
    else if ((center2[1] - top2[1]) / (data[36][1] - center2[1]) > 0.96) {
        verticalRight = 2;
    }

    /**
     * horizontal
     */
    // horizontal left
    const leftDiff = right1[0] - center1[0];
    const rightDiff = center2[0] - left2[0];
    console.log('左:' + leftDiff);
    console.log('左眼大小:' + x1)
    console.log('左比:' + leftDiff / x1)
    console.log('右:' + rightDiff)
    console.log('右眼大小:' + x2)
    console.log('右比:' + rightDiff / x2)
    // if ((center1[0] - left1[0]) < x1 * 0.5295 && (center1[0] - left1[0]) > x1 * 0.516) {
    if (leftDiff < x1 * 0.484 && leftDiff > x1 * 0.475 || leftDiff < x1 * 0.46) {
        horizontalLeft = 1;
    }
    // else if ((center1[0] - left1[0]) < x1 * 0.6) {
    //     horizontalLeft = 2;
    // }
    // horizontal right
    if (rightDiff < x2 * 0.54 && rightDiff > 0.505) {
        horizontalRight = 1;
    }
    // else if ((center2[0] - left2[0]) > x2 * 0.6) {
    //     horizontalRight = 2;
    // }

    return [openLeft, openRight, verticalLeft, verticalRight, horizontalLeft, horizontalRight];

}

/**
 * 张闭嘴、嘴角左、嘴角右
 * @param data
 */
function mouth(data) {
    let [open, cornerLeft, cornerRight, isBig] = [0, 0, 0, 0];
    const [left, right, top1, top2, bottom1, bottom2] = [
        data[58][1],
        data[62][1],
        Math.min(data[59][1], data[60][1]),
        Math.max(data[66][1], data[67][1]),
        Math.min(data[64][1], data[65][1]),
        Math.max(data[70][1], data[71][1])
    ];

    // 唇的厚度为基准
    const y1 = top2 - top1;
    const y2 = bottom1 - bottom2;
    const y = Math.max(y1, y2);

    const topCenter = top1 + (top2 - top1) / 2;
    const halfY = (bottom1 - top1);

    // 张嘴
    console.log('距离:' + (bottom2 - top2))
    console.log('唇厚:' + y)
    console.log('比值:' + (bottom2 - top2) / y)
    if (bottom2 - top2 > y) {
        open = 1;
    }

    // 张大嘴
    if (bottom2 - top2 > y * 2.5) {
        isBig = 1;
    }

    // 嘴角 left
    if (left < topCenter) {
        cornerLeft = 1;
    }
    else if (left > bottom2) {
        cornerLeft = 2;
    }

    // 嘴角 right
    if (right < topCenter) {
        cornerRight = 1;
    }
    else if (right > bottom2) {
        cornerRight = 2;
    }

    return [open, cornerLeft, cornerRight, isBig];
}

function eyebrow(data) {
    let [left, right] = [0, 0];
    const [leftY, rightY] = [Math.min(data[23][1], data[24][1], data[25][1]), Math.min(data[40][1], data[41][1], data[42][1])];
    console.log(leftY)
    console.log(rightY)
    if (leftY < rightY - 0.1) {
        left = 1;
    }
    if (leftY > rightY + 0.1) {
        right = 1;
    }
    return [left, right];
}
