package com.jachdev.consumerprotection.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.jachdev.commonlibs.base.BaseFragment;
import com.jachdev.commonlibs.widget.CustomTextView;
import com.jachdev.consumerprotection.R;
import com.jachdev.consumerprotection.data.User;
import com.jachdev.consumerprotection.data.enums.UserType;
import com.jachdev.consumerprotection.util.SessionManager;

import org.jetbrains.annotations.NotNull;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import butterknife.BindView;
import butterknife.OnClick;

public class HomeFragment extends BaseFragment {

    @BindView(R.id.tv_header)
    CustomTextView tvHeader;

    private HomeViewModel homeViewModel;
    private FragmentEventListener listener;

    @Override
    protected int layoutRes() {
        return R.layout.fragment_home;
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        init();

        homeViewModel.getUser().observe(getViewLifecycleOwner(), mUserObserver);
    }

    private void init() {
        UserType userType = SessionManager.getInstance().getUser().getUserType();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        listener = (FragmentEventListener) context;
    }

    private Observer<? super User> mUserObserver = new Observer<User>() {
        @Override
        public void onChanged(@Nullable User user) {
            String header = getString(R.string.hello) + " " + user.getName();
            tvHeader.setAnyText(header);
        }
    };

    @OnClick(R.id.btn_image_rec)
    public void onImageRecognitionClick(){
        if(listener != null)
            listener.onImageRecognitionClick();
    }

    @OnClick(R.id.btn_calorie_count)
    public void onCalorieCounterClick(){
        if(listener != null)
            listener.onCalorieCounterClick();
    }

    @OnClick(R.id.btn_meal_plan)
    public void onMealPlanClick(){
        if(listener != null)
            listener.onMealPlanClick();
    }

    public interface FragmentEventListener{
        void onImageRecognitionClick();
        void onCalorieCounterClick();
        void onMealPlanClick();
    }
}