package com.yonge.cooleshow.biz.dal.wrapper;

import com.alibaba.excel.annotation.ExcelProperty;
import com.yonge.cooleshow.biz.dal.entity.SysUser;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 学生封装类
 * Created by Eric.Shang on 2023/1/6.
 */
public class StudentWrapper {

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel("学生帐号关联")
    public static class UnionStudent implements Serializable {

        @ApiModelProperty("帐号关联Id")
        private Long unionId;

        @ApiModelProperty("授权Token")
        private String token;

        @ApiModelProperty("联系方式")
        private String mobile;

        @ApiModelProperty("声部名称")
        private String subjectName;

        @ApiModelProperty("用户名")
        private String username;

        @ApiModelProperty("性别（0，女  1，男）")
        private Integer gender;

        @ApiModelProperty("是否校验token")
        private Boolean tokenCheck;

        @ApiModelProperty("0-正常，1-锁定")
        private Integer lockFlag;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel("帐号关联响应")
    public static class UnionStudentResp implements Serializable {

        @ApiModelProperty("帐号关联Id")
        private Long unionId;

        @ApiModelProperty("联系方式")
        private String mobile;

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

        @ApiModelProperty("用户名")
        private String username;

        @ApiModelProperty("更新标识")
        private Boolean updateFlag;

        private SysUser sysUser;

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

        public UnionStudentResp username(String username) {
            this.username = username;
            return this;
        }
    }

    @Data
    @ApiModel("学生基础信息")
    public static class Student {

        @ApiModelProperty("id")
        private Long id;

        @ApiModelProperty("所属机构，开放接口必填")
        private Long tenantId;

        @ApiModelProperty("机构小组ID")
        private Long tenantGroupId;

        @ApiModelProperty("姓名")
        private String name;

        @ApiModelProperty("性别。0：女 1：男")
        private Integer gender;

        @ApiModelProperty("生日")
        private LocalDate birthdate;

        @ApiModelProperty("手机号码")
        private String phone;

        @ApiModelProperty("头像")
        private String avatar;

        @ApiModelProperty("声部，多个用逗号隔开")
        private String subjectId;

//        @ApiModelProperty("是否解绑")
//        private Boolean bindTenant;

    }

    @Data
    @ApiModel("学生导入模板模型")
    public static class StudentImport {

        @ExcelProperty(value = "学生姓名")
        private String userName;

        @ExcelProperty(value = "手机号")
        private String phone;

        @ExcelProperty(value = "声部")
        private String subjectName;

        @ExcelProperty(value = "出生日期")
        private String birthday;

        @ExcelProperty(value = "性别")
        private String gender;

        @ExcelProperty(value = "小组")
        private String tenantGroupName;

        private static final String PHONE_REG = "^1\\d{10}$";

        public List<String> checkValid() {
            ArrayList<String> errMsg = new ArrayList<>();
            if (StringUtils.isEmpty(userName)) {
                errMsg.add("姓名为空");
            }
            if (StringUtils.isEmpty(phone)) {
                errMsg.add("手机号为空");
            } else if (!Pattern.matches(PHONE_REG, phone)) {
                errMsg.add("手机号格式错误");
            }
            if (StringUtils.isEmpty(subjectName)) {
                errMsg.add("声部为空");
            }
            if (StringUtils.isEmpty(birthday)) {
                errMsg.add("出生日期为空");
            } else {
                if (birthday.contains("/")) {
                    birthday = birthday.replaceAll("/", "-");
                }
                try {
                    LocalDate.parse(birthday, DateTimeFormatter.ISO_LOCAL_DATE);
                } catch (Exception e) {
                    errMsg.add("出生日期格式错误");
                }
            }
            if (StringUtils.isEmpty(gender)) {
                errMsg.add("性别为空");
            } else if (!"男".equals(gender) && !"女".equals(gender)) {
                errMsg.add("性别错误");
            }
            if (StringUtils.isEmpty(tenantGroupName)) {
                errMsg.add("小组为空");
            }
            return errMsg;
        }
    }

    @Data
    public static class ExportStudentTemplate {

        @ExcelProperty(value = "姓名")
        private String userName;

        @ExcelProperty(value = "手机号")
        private String phone;

        @ExcelProperty(value = "声部")
        private String subjectName;

        @ExcelProperty(value = "小组")
        private String tenantGroupName;
    }

    @Data
    @ApiModel("学生修改机构")
    public static class StudentUpdateTenant {

        @ApiModelProperty("学生ID")
        private Long studentId;

        @ApiModelProperty("机构ID")
        private Long tenantId;
    }

    @Data
    @ApiModel("学生修改密码")
    public static class StudentUpdatePassword {

        @ApiModelProperty("密码")
        @NotNull
        private String password;
    }

    @Data
    public static class UserCount {
        private Long tenantGroupId;
        private Integer count;
    }
}
