package ch.pizzapp.android.helper;

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

import ch.pizzapp.android.model.CheckCouponModel;
import ch.pizzapp.android.model.CustomerTokenModel;
import ch.pizzapp.android.model.StoreModel;
import ch.pizzapp.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(CheckCouponModel couponModel){
        SharedPrefsHelper.saveSelectedCoupon(couponModel);
    }

    public static CheckCouponModel getSelectedCoupon(){
        return SharedPrefsHelper.getSelectedCoupon();
    }

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

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