package com.android.oleksandrpriadko.cookbrowser.fragments.dish;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;

import com.android.oleksandrpriadko.cookbrowser.R;
import com.android.oleksandrpriadko.cookbrowser.adapter.recycler_view.AdapterDishComponents;
import com.android.oleksandrpriadko.cookbrowser.constants.BUNDLE;
import com.android.oleksandrpriadko.cookbrowser.fragments.FragBaseAbstract;
import com.android.oleksandrpriadko.cookbrowser.item_decoration.ItemDecorLinVertSpac;
import com.android.oleksandrpriadko.cookbrowser.structure.Dish;
import com.backendless.Backendless;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.exceptions.BackendlessFault;

public class FragPagerDishComponents extends FragBaseAbstract {
    private static final int ROOT_LAYOUT_ID = R.layout.frag_dish_components_page;

    private AdapterDishComponents adapterDishComponents;
    private FrameLayout layoutLoading;
    private FrameLayout layoutEmptyContent;

    @Override
    protected int getRootLayoutId() {
        return ROOT_LAYOUT_ID;
    }

    @Override
    protected void prepareRoot(View rootView) {
        initLoadingLayout(rootView);
        initEmptyContentLayout(rootView);
        initRecViewComponents(rootView);
    }

    private void initLoadingLayout(View rootView) {
        layoutLoading = ((FrameLayout) rootView.findViewById(R.id.layout_loading));
    }

    private void initEmptyContentLayout(View rootView) {
        layoutEmptyContent = ((FrameLayout) rootView.findViewById(R.id.layout_empty_content));
    }

    private void initRecViewComponents(View rootView) {
        RecyclerView recViewComponents
                = ((RecyclerView) rootView.findViewById(R.id.recView_dish_components));
//        ItemDecorLinVertSpac itemDecorLinVertSpac = new ItemDecorLinVertSpac(
//                10,
//                10,
//                10,
//                10,
//                true,
//                true);
//        recViewComponents.addItemDecoration(itemDecorLinVertSpac);
        recViewComponents.setHasFixedSize(true);
        LinearLayoutManager layoutManager
                = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recViewComponents.setLayoutManager(layoutManager);
        adapterDishComponents = new AdapterDishComponents(null, componentListener);
        recViewComponents.setAdapter(adapterDishComponents);
        if (getArguments() != null && getArguments().getString(BUNDLE.STRING.DISH_OBJECT_ID) != null) {
            getDishById(getArguments().getString(BUNDLE.STRING.DISH_OBJECT_ID));
        } else {
            showEmptyContent(true);
        }
    }

    private void getDishById(String objectId) {
        showLoading(true);
        Backendless.Data.of(Dish.class).findById(objectId, getDishByIdCall);
    }

    private void showLoading(boolean show) {
        layoutLoading.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    private void showEmptyContent(boolean show) {
        layoutEmptyContent.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    /////////////////////////////////////////////////////////////////////////////////////////////
    //  LISTENERS
    //////////////////////////////////////////////////////////////////////////////////////////////
    private AdapterDishComponents.OnItemClickListener componentListener
            = new AdapterDishComponents.OnItemClickListener() {
        @Override
        public void itemClicked(int position) {

        }
    };

    private AsyncCallback<Dish> getDishByIdCall = new AsyncCallback<Dish>() {
        @Override
        public void handleResponse(Dish response) {
            showLoading(false);
            if (!response.getComponentsList().isEmpty()) {
                showEmptyContent(false);
                adapterDishComponents.setComponents(response.getComponentsList());
                adapterDishComponents.notifyDataSetChanged();
            } else {
                showEmptyContent(true);
            }
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            showLoading(false);
            showEmptyContent(true);
        }
    };
}
