package com.yonge.cooleshow.biz.dal.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yonge.cooleshow.biz.dal.dao.SysMusicCompareRecordDao;
import com.yonge.cooleshow.biz.dal.entity.MusicSheet;
import com.yonge.cooleshow.biz.dal.entity.SysMusicCompareRecord;
import com.yonge.cooleshow.biz.dal.enums.FeatureType;
import com.yonge.cooleshow.biz.dal.service.MusicPracticeRecordService;
import com.yonge.cooleshow.biz.dal.service.MusicSheetService;
import com.yonge.cooleshow.biz.dal.service.SysMusicCompareRecordService;
import com.yonge.cooleshow.biz.dal.wrapper.MusicPracticeRecordWrapper;
import com.yonge.toolset.base.exception.BizException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service
public class MusicPracticeRecordServiceImpl implements MusicPracticeRecordService {

    @Autowired
    private SysMusicCompareRecordService sysMusicCompareRecordService;

    @Autowired
    private MusicSheetService musicSheetService;

    @Autowired
    private SysMusicCompareRecordDao sysMusicCompareRecordDao;

    /**
     * 查询记录详情
     *
     * @param id
     */
    @Override
    public MusicPracticeRecordWrapper.Entity detail(Long id) {
        SysMusicCompareRecord sysMusicCompareRecord = sysMusicCompareRecordService.get(id);
        if (sysMusicCompareRecord == null) {
            throw new BizException("记录不存在");
        }
        return MusicPracticeRecordWrapper.Entity.toEntity(sysMusicCompareRecord);

    }

    /**
     * 添加记录
     *
     * @param from
     */
    @Override
    public String add(MusicPracticeRecordWrapper.MusicPracticeRecord from) {


        // 曲目练习记录
        from.setCreateTime(new Date());
        MusicPracticeRecordWrapper.Entity musicPracticeRecord = JSON.parseObject(from.initDefaultValue().jsonString(),
            MusicPracticeRecordWrapper.Entity.class);
        SysMusicCompareRecord sysMusicCompareRecord = musicPracticeRecord.toSysMusicCompareRecord();
        if (sysMusicCompareRecord.getFeature() == FeatureType.CLOUD_STUDY_EVALUATION) {
            sysMusicCompareRecordService.saveMusicCompareData(sysMusicCompareRecord);
        } else {
            sysMusicCompareRecordService.insert(sysMusicCompareRecord);
        }

        return String.valueOf(sysMusicCompareRecord.getId());
    }

    @Override
    public Object getLastEvaluationMusicalNotesPlayStats(Long recordId) {
        MusicPracticeRecordWrapper.Entity userLastEvaluationData = detail(recordId);
        if(Objects.isNull(userLastEvaluationData)){
            return null;
        }
        if(StringUtils.isBlank(userLastEvaluationData.getScoreData())){
            return null;
        }

        JSONObject jsonObject = JSON.parseObject(userLastEvaluationData.getScoreData());
        jsonObject.put("recordId", userLastEvaluationData.getId());
        jsonObject.put("score", userLastEvaluationData.getScore());
        jsonObject.put("cadence", userLastEvaluationData.getCadence());
        jsonObject.put("intonation", userLastEvaluationData.getIntonation());
        jsonObject.put("integrity", userLastEvaluationData.getIntegrity());
        jsonObject.put("heardLevel", userLastEvaluationData.getHeardLevel());
        jsonObject.put("videoFilePath", userLastEvaluationData.getVideoFilePath());
        jsonObject.put("partIndex", userLastEvaluationData.getPartIndex());
        jsonObject.put("customConfiguration", userLastEvaluationData.getCustomConfiguration());

        MusicSheet sysMusicScore = musicSheetService.getById(Integer.parseInt(userLastEvaluationData.getMusicSheetId()));
        if(Objects.nonNull(sysMusicScore)){
            jsonObject.put("sysMusicScoreName", sysMusicScore.getMusicSheetName());
        }
        return jsonObject;
    }

    /**
     * 查询记录
     *
     * @param id
     */
    @Override
    public MusicPracticeRecordWrapper.Entity getById(Long id) {
        return detail(id);
    }

    /**
     * 更新练习记录
     *
     * @param practiceRecord
     */
    @Override
    public void update(MusicPracticeRecordWrapper.Entity practiceRecord) {

        SysMusicCompareRecord sysMusicCompareRecord = practiceRecord.toSysMusicCompareRecord();
        sysMusicCompareRecordService.update(sysMusicCompareRecord);
    }
}
