package ch.pizzalemon.android.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;


/**
 * Created by cimenmus on 6.02.2018.
 */

public class GeneralCouponModel extends CouponModel {

    @Expose
    @SerializedName("coupon_id")
    private String couponId;

    @Expose
    @SerializedName("product")
    private ArrayList<String> productIdList;

    @Expose
    @SerializedName("date_start")
    private String startDate;

    @Expose
    @SerializedName("date_end")
    private String endDate;

    @Expose
    @SerializedName("uses_total")
    private String totalUsageCount;

    @Expose
    @SerializedName("uses_customer")
    private String userUsageCount;

    private String name;
    private String type;
    private String discount;
    private String shipping;
    private String total;

    public enum CouponDiscountType {
        FIXED,
        PERCENT
    }

    @Override
    public CouponType getCouponType() {
        return CouponType.GENERAL;
    }

    public String getCouponId() {
        if(couponId == null){
            couponId = "";
        }
        return couponId;
    }

    public ArrayList<String> getProductIdList() {
        if(productIdList == null){
            productIdList = new ArrayList<>();
        }
        return productIdList;
    }

    public String getStartDate() {
        if(startDate == null){
            startDate = "1970-01-01";
        }
        return startDate;
    }

    public String getEndDate() {
        if(endDate == null){
            endDate = "1970-01-01";
        }
        return endDate;
    }

    public String getTotalUsageCount() {
        if(totalUsageCount == null){
            totalUsageCount = "";
        }
        return totalUsageCount;
    }

    public String getUserUsageCount() {
        if(userUsageCount == null){
            userUsageCount = "";
        }
        return userUsageCount;
    }

    public String getName() {
        if(name == null){
            name = "";
        }
        return name;
    }

    public String getType() {
        if(type == null){
            type = "";
        }
        return type;
    }

    public String getDiscount() {
        if(discount == null){
            discount = "";
        }
        return discount;
    }

    public String getShipping() {
        if(shipping == null){
            shipping = "";
        }
        return shipping;
    }

    public String getTotal() {
        if(total == null){
            total = "";
        }
        return total;
    }

    public CouponDiscountType getCouponDiscountType(){
        if(type.toLowerCase().equals("f")){
            return CouponDiscountType.FIXED;
        }
        else {
            return CouponDiscountType.PERCENT;
        }
    }

    @Override
    public boolean isActive(){
        return getStatus().equals("1") && isCouponUsable();
    }

    private boolean isCouponUsable(){

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendarEnd = null;
        Calendar calendarNow = Calendar.getInstance();
        try {
            Date dateEnd = formatter.parse(endDate);
            calendarEnd = Calendar.getInstance();
            calendarEnd.setTime(dateEnd);

            if(calendarEnd.get(Calendar.YEAR) > calendarNow.get(Calendar.YEAR)){
                return true;
            }

            if(calendarEnd.get(Calendar.MONTH) > calendarNow.get(Calendar.MONTH)){
                return true;
            }

            if(calendarEnd.get(Calendar.DAY_OF_MONTH) >= calendarNow.get(Calendar.DAY_OF_MONTH)){
                return true;
            }

            return false;


        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

}
