package ch.pizzalemon.android.helper;

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

import ch.pizzalemon.android.model.AddressModel;
import ch.pizzalemon.android.model.CouponModel;
import ch.pizzalemon.android.model.CouponType;
import ch.pizzalemon.android.model.GeneralCouponModel;
import ch.pizzalemon.android.model.CustomerTokenModel;
import ch.pizzalemon.android.model.PersonalCouponModel;
import ch.pizzalemon.android.model.StoreModel;
import ch.pizzalemon.android.model.UserModel;

/**
 * Created by cimenmus on 11/09/2017.
 */

public class SessionHelper {

    public static void saveCustomer(UserModel user){
        SharedPrefsHelper.saveUser(user);
    }

    public static UserModel getUser(){
        return SharedPrefsHelper.getUser();
    }

    public static void clearCustomerInfo(){
        SharedPrefsHelper.clearCustomerInfo();
    }

    public static void saveCustomerToken(CustomerTokenModel customerToken){
        SharedPrefsHelper.saveCustomerToken(customerToken);
    }

    public static CustomerTokenModel getCustomerToken(){
        return SharedPrefsHelper.getCustomerToken();
    }

    public static void clearCustomerToken(){
        SharedPrefsHelper.clearCustomerToken();
    }

    public static void setCustomerLoggedIn(boolean loggedId){
        SharedPrefsHelper.setCustomerLoggedIn(loggedId);
    }

    public static boolean isCustomerLoggedIn(){
        return SharedPrefsHelper.isCustomerLoggedIn() &&
                SharedPrefsHelper.getCustomerToken().getStoreOfToken().getStoreName().toLowerCase().equals(
                        SharedPrefsHelper.getSelectedStore().getStoreName().toLowerCase());
    }

    public static void setSelectedStore(StoreModel storeModel){
        SharedPrefsHelper.saveSelectedStore(storeModel);
    }

    public static StoreModel getSelectedStore(){
        StoreModel selectedStore = SharedPrefsHelper.getSelectedStore();
        return selectedStore;
       //return SharedPrefsHelper.getSelectedStore();
    }

    public static void setIsFirstTime(boolean isFirstTime){
        SharedPrefsHelper.setIsFirstTime(isFirstTime);
    }

    public static boolean isFirstTime(){
        return SharedPrefsHelper.isFirstTime();
    }

    public static void logOut(){
        setCustomerLoggedIn(false);
        clearCustomerToken();
        clearCustomerInfo();
    }

    public static void saveSelectedCoupon(CouponModel couponModel){
        if(couponModel instanceof PersonalCouponModel){
            PersonalCouponModel personalCouponModel = (PersonalCouponModel) couponModel;
            SessionHelper.clearSelectedCoupon();
            SharedPrefsHelper.saveSelectedPersonalCoupon(personalCouponModel);
        }
        else if(couponModel instanceof GeneralCouponModel){
            GeneralCouponModel generalCouponModel = (GeneralCouponModel) couponModel;
            SessionHelper.clearSelectedCoupon();
            SharedPrefsHelper.saveSelectedGeneralCoupon(generalCouponModel);
        }
    }

    public static CouponModel getSelectedCoupon(){
        if(SharedPrefsHelper.getSelectedPersonalCoupon() != null){
            return SharedPrefsHelper.getSelectedPersonalCoupon();
        }
        else {
            return SharedPrefsHelper.getSelectedGeneralCoupon();
        }
    }

    public static void clearSelectedCoupon(){
        SharedPrefsHelper.clearSelectedGeneralCoupon();
        SharedPrefsHelper.clearSelectedPersonalCoupon();
    }

    public static void addCouponCodeToRequestParamsIfNeeded(HashMap<String, Object> params){
        CouponModel selectedCoupon = SessionHelper.getSelectedCoupon();
        if(SessionHelper.getSelectedCoupon() != null){
            if(selectedCoupon.getCouponType() == CouponType.PERSONAL){
                params.put("voucher", SessionHelper.getSelectedCoupon().getCode());
            }
            else if(selectedCoupon.getCouponType() == CouponType.GENERAL){
                params.put("coupon", SessionHelper.getSelectedCoupon().getCode());
            }
        }
    }

    public static String getSessionId(){
        return SharedPrefsHelper.getSessionId();
    }

    public static void saveSessionId(String sessionId){
        SharedPrefsHelper.saveSessionId(sessionId);
    }

    public static void saveUserMainAddress(AddressModel addressModel){
        SharedPrefsHelper.saveUserMainAddress(addressModel);
    }

    public static AddressModel getUserMainAddress(){
        return SharedPrefsHelper.getUserMainAddress();
    }

    public static Calendar getTokenDeathDate(String tokenDeathTime){

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = null;
        try {
            Date date = formatter.parse(tokenDeathTime);
            calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar;
        } catch (ParseException e) {
            e.printStackTrace();
            return calendar;
        }
    }
}
