package com.sheep.gamegroup.model.entity;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.sheep.gamegroup.util.TimeUtil;

import java.util.Calendar;

/**
 * Created by realicing on 2018/11/13.
 * realicing@sina.com
 */
public class UserSign implements Comparable<UserSign> {

    /**
     * 用户ID
     * user_id : 63201
     * 签到周期
     * day : 1
     * 签到日期
     * sign_date : 2018-11-05
     * 是否补签
     * is_supplement : 0
     */

    private int user_id;
    private int day;
    private String sign_date;
    private int is_supplement;
    private int has_share;

    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public String getSign_date() {
        return sign_date;
    }

    public void setSign_date(String sign_date) {
        this.sign_date = sign_date;
    }

    public int getIs_supplement() {
        return is_supplement;
    }

    public void setIs_supplement(int is_supplement) {
        this.is_supplement = is_supplement;
    }

    public int getHas_share() {
        return has_share;
    }

    public void setHas_share(int has_share) {
        this.has_share = has_share;
    }



    @Override
    public boolean equals(Object obj) {
        return obj instanceof UserSign && ((UserSign) obj).day == day;//这里认为，day相同， 就是同一天的数据
    }

    public boolean isToday() {
        return TextUtils.equals(sign_date, getSignDateByTime(System.currentTimeMillis()));
    }

    //当天是否进行过补签
    public boolean signSupplemented(){
        return has_share >= SHARE_TYPE_SIGN_SUPPLEMENT;
    }
    //当天是否进行过分享
    public boolean signShared(){
        return has_share >= SHARE_TYPE_SHARE;
    }
    @Override
    public int compareTo(@NonNull UserSign userSign) {
        return day - userSign.getDay();
    }

    //让当前的签到时间与otherUserSign的签到时间统一起来
    public void resetSignDate(UserSign otherUserSign) {
        String signDate = otherUserSign.getSign_date();
        int year = NumberFormatUtils.parseInteger(signDate.substring(0, 4));
        int month = NumberFormatUtils.parseInteger(signDate.substring(5, 7));
        int day = NumberFormatUtils.parseInteger(signDate.substring(8));
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);//这里要-1，很容易搞错
        calendar.set(Calendar.DAY_OF_MONTH, day);
        int compareTo = compareTo(otherUserSign);
        calendar.add(Calendar.DAY_OF_MONTH, compareTo);
        setSign_date(getSignDateByTime(calendar.getTimeInMillis()));
    }

    //通过时间获取对应的日期格式
    public static String getSignDateByTime(long time) {
        return TimeUtil.TimeStamp2Date(time, "yyyy-MM-dd");
    }

    public static final int SHARE_TYPE_SHARE = 1;//已经分享
    public static final int SHARE_TYPE_SIGN_SUPPLEMENT = 2;//已经补签
}
