package com.yonge.cooleshow.admin.io.request.music;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.HeardLevelEnum;
import com.yonge.cooleshow.biz.dal.enums.MK;
import com.yonge.cooleshow.biz.dal.enums.music.MusicCompareType;
import com.yonge.toolset.base.page.QueryInfo;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * Created by Eric.Shang on 2022/10/17.
 */
public class MusicCompareVo implements Serializable {

    /**
     * 统计汇总查询条件
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @EqualsAndHashCode(callSuper = false)
    public static class StatQueryRequest extends QueryInfo {

        @ApiModelProperty("客户端类型 TEACHER(老师端) STUDENT(学生端)")
        private ClientEnum clientType;

        @ApiModelProperty("用户ID ")
        private Long userId;

        @ApiModelProperty("排序类型 TRAIN_DURATION(练习时长) TRAIN_FREQ(练习次数) EVALUATE_DURATION(评测时长) EVALUATE_FREQ(评测次数)")
        private MusicCompareType compareType;

        @ApiModelProperty("排序方式 升序(ASC) 降序(DESC)")
        private String sortType;

        @ApiModelProperty("曲目编号/名称")
        private String keyword;

        @ApiModelProperty("开始时间")
        @JsonFormat(pattern = MK.TIME_PATTERN, timezone = MK.TIME_ZONE)
        private Long startTime;

        @ApiModelProperty("结束时间")
        @JsonFormat(pattern = MK.TIME_PATTERN, timezone = MK.TIME_ZONE)
        private Long endTime;

        @ApiModelProperty(value = "曲目ID", hidden = true)
        private Long musicId;
        @ApiModelProperty(value = "曲目名称", hidden = true)
        private String musicName;

        public boolean invalidParam() {

            return Objects.isNull(getClientType())
                    || Objects.isNull(getUserId())
                    || ClientEnum.invalid(getClientType().getCode());
        }

        public String jsonString() {

            return JSON.toJSONString(this);
        }

        public MusicCompareType getCompareType() {
            return Optional.ofNullable(compareType).orElse(MusicCompareType.EVALUATE_DURATION);
        }

        public String getSortType() {
            return Optional.ofNullable(sortType).orElse("DESC");
        }

        public void setKeyword(String keyword) {
            if (StringUtils.isNotEmpty(keyword)) {

                if (keyword.matches(MK.EXP_INT)) {

                    setMusicId(Long.parseLong(keyword));
                } else {
                    setMusicName(keyword);
                }
            }
            this.keyword = keyword;
        }

        public StatQueryRequest userId(Long userId) {
            this.userId = userId;
            return this;
        }

    }

    /**
     * 曲目统计汇总信息
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class RecordStat implements Serializable {

        @ApiModelProperty("练习曲目总数")
        private Long trainMusicNums;

        @ApiModelProperty("使用时长")
        private Long trainDuration;

        @ApiModelProperty("使用次数")
        private Long trainFrequency;

        @ApiModelProperty("评测曲目总数")
        private Long evaluateMusicNums;

        @ApiModelProperty("评测时长")
        private Long evaluateDuration;

        @ApiModelProperty("评测次数")
        private Long evaluateFrequency;

        private List<RecordInfo> recordInfos;

        public static RecordStat from(String record) {

            return JSON.parseObject(record, RecordStat.class);
        }
    }

    /**
     * 曲目统计信息
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class RecordInfo implements Serializable {

        @ApiModelProperty("曲目ID")
        private Long musicSheetId;

        @ApiModelProperty("曲目名称")
        private String musicName;

        @ApiModelProperty("使用时长")
        private Long trainDuration;

        @ApiModelProperty("使用次数")
        private Long trainFrequency;

        @ApiModelProperty("评测时长")
        private Long evaluateDuration;

        @ApiModelProperty("评测次数")
        private Long evaluateFrequency;

        @ApiModelProperty("最近使用时间")
        @JsonFormat(pattern = MK.TIME_PATTERN, timezone = MK.TIME_ZONE)
        private Date recentTime;

        @ApiModelProperty("作曲人")
        private String composer;

        @ApiModelProperty("音乐标签")
        private String musicTagName;
    }

    /**
     * 评测记录查询
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @EqualsAndHashCode(callSuper = false)
    public static class EvaluateQueryInfo extends QueryInfo {

        @ApiModelProperty("客户端类型 TEACHER(老师端) STUDENT(学生端)")
        private ClientEnum clientType;

        @ApiModelProperty("用户ID ")
        private Long userId;

        @ApiModelProperty("曲目ID ")
        private Long musicId;

        @ApiModelProperty("评测难度")
        private HeardLevelEnum heardLevel;

        @ApiModelProperty("开始时间")
        @JsonFormat(pattern = MK.TIME_PATTERN, timezone = MK.TIME_ZONE)
        private Long startTime;

        @ApiModelProperty("结束时间")
        @JsonFormat(pattern = MK.TIME_PATTERN, timezone = MK.TIME_ZONE)
        private Long endTime;

        public boolean invalidParam() {

            return Objects.isNull(getClientType())
                    || Objects.isNull(getUserId())
                    || Objects.isNull(getMusicId());
        }

        public String jsonString() {

            return JSON.toJSONString(this);
        }
    }
}
