package ch.pizzaleu.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 CheckCouponModel {

    @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;

    @Expose
    @SerializedName("date_added")
    private String addedDate;

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


    private String storeName;

    public void checkNull(){

        if(couponId == null){
            couponId = "";
        }

        if(startDate == null){
            startDate = "1970-01-01";
        }

        if(endDate == null){
            endDate = "1970-01-01";
        }

        if(totalUsageCount == null){
            totalUsageCount = "";
        }

        if(userUsageCount == null){
            userUsageCount = "";
        }

        if(addedDate == null){
            addedDate = "";
        }

        if(code == null){
            code = "";
        }

        if(name == null){
            name = "";
        }

        if(type == null){
            type = "";
        }

        if(discount == null){
            discount = "";
        }

        if(shipping == null){
            shipping = "";
        }

        if(total == null){
            total = "";
        }

        if(status == null){
            status = "";
        }

        if(productIdList == null){
            productIdList = new ArrayList<>();
        }

        if(storeName == null){
            storeName = "";
        }
    }

    public enum CouponDiscountType {
        FIXED,
        PERCENT
    }

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

    public boolean isActive(){
        return status.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;
        }
    }

    public String getCouponId() {
        return couponId;
    }

    public void setCouponId(String couponId) {
        this.couponId = couponId;
    }

    public ArrayList<String> getProductIdList() {
        return productIdList;
    }

    public void setProductIdList(ArrayList<String> productIdList) {
        this.productIdList = productIdList;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public String getTotalUsageCount() {
        return totalUsageCount;
    }

    public void setTotalUsageCount(String totalUsageCount) {
        this.totalUsageCount = totalUsageCount;
    }

    public String getUserUsageCount() {
        return userUsageCount;
    }

    public void setUserUsageCount(String userUsageCount) {
        this.userUsageCount = userUsageCount;
    }

    public String getAddedDate() {
        return addedDate;
    }

    public void setAddedDate(String addedDate) {
        this.addedDate = addedDate;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDiscount() {
        return discount;
    }

    public void setDiscount(String discount) {
        this.discount = discount;
    }

    public String getShipping() {
        return shipping;
    }

    public void setShipping(String shipping) {
        this.shipping = shipping;
    }

    public String getTotal() {
        return total;
    }

    public void setTotal(String total) {
        this.total = total;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStoreName() {
        return storeName;
    }

    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }
}
