package com.sagacloud.endpointService.pojo;

import com.alibaba.fastjson.JSONArray;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by wo on 8/21/19.
 */
public class CalendarRule {
    static final String D_OF_W = "dOfW";
    static final String D_OF_M = "dOfM";
    static final String M_AND_D = "mAndD";

    private String tag;

    //dOfW/dOfM/mAndD
    private String type;

    //dOfW  值格式 ["1", "2", "3"]
    //dOfW  值格式 ["1", "2", "10-18"]
    //mAndD 值格式 ["0221", "0822", "0910-1018", "1210-0118"]
    private JSONArray values;

    private Set<Integer> nums = new HashSet<>();

    public CalendarRule(String tag, String type, JSONArray values){
        super();
        this.tag = tag;
        this.type = type;
        this.values = values;
        if(this.values != null){
            this.parseV();
        }
    }

    private void parseV(){
        switch (this.type){
            case D_OF_W:
                for (int i = 0; i < this.values.size(); i++){
                    this.nums.add(this.values.getInteger(i));
                }
                break;
            case D_OF_M:
                for (int i = 0; i < this.values.size(); i++){
                    String str = this.values.getString(i);
                    if(str.length() > 2 && str.contains("-")){
                        String[] arr = str.split("-");
                        for(int j = Integer.valueOf(arr[0]); j <= Integer.valueOf(arr[1]); j++){
                            this.nums.add(j);
                        }
                    }else{
                       this.nums.add(Integer.valueOf(str));
                    }
                }
                break;
            case M_AND_D:
                LocalDateTime d = LocalDateTime.now();
                for (int i = 0; i < this.values.size(); i++){
                    String str = this.values.getString(i).trim();
                    if(str.length() != 4 && !str.contains("-")){
                        continue;
                    }
                    if(str.length() == 4){
                        d = d.withMonth(Integer.valueOf(str.substring(0, 2))).withDayOfMonth(Integer.valueOf(str.substring(2)));
                        this.nums.add(d.getDayOfYear());
                        continue;
                    }
                    if(str.contains("-")){
                        String[] arr = str.split("-");
                        if(arr.length !=2 || arr[0].trim().length() != 4 || arr[1].trim().length() != 4){
                            continue;
                        }
                        LocalDateTime from = d.withMonth(Integer.valueOf(arr[0].trim().substring(0, 2))).withDayOfMonth(Integer.valueOf(arr[0].trim().substring(2)));
                        LocalDateTime to = d.withMonth(Integer.valueOf(arr[1].trim().substring(0, 2))).withDayOfMonth(Integer.valueOf(arr[1].trim().substring(2)));
                        if(to.isBefore(from)){
                            to = to.plusYears(1);
                        }
                        to = to.plusDays(1);
                        while(from.isBefore(to)){
                            this.nums.add(from.getDayOfYear());
                            from = from.plusDays(1);
                        }
                    }
                }
                break;
        }

    }

    /**判断某天是否match该rule
     * @param date
     * @return
     */
    public boolean isMatching(LocalDateTime date){
        if(this.type.equals(D_OF_W)){
            if(this.nums.contains(date.getDayOfWeek().getValue())){
                return true;
            }
        }else if(this.type.equals(D_OF_M)){
            if(this.nums.contains(date.getDayOfMonth())){
                return true;
            }
        }else {
            if(this.nums.contains(date.getDayOfYear())){
                return true;
            }
        }
        return false;
    }

    public String getTag() {
        return tag;
    }
}
