package com.prankapp.fartsounds.airhorn.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.prankapp.fartsounds.airhorn.R;
import com.prankapp.fartsounds.airhorn.adapterdfds.PrankListAdapter;
import com.prankapp.fartsounds.airhorn.model.PrankListModel;
import com.prankapp.fartsounds.airhorn.utilsfff.Constant;
import com.prankapp.fartsounds.airhorn.utilsfff.ImmUtils;
import com.prankapp.fartsounds.airhorn.utilsfff.JsonUtils;

import java.util.ArrayList;

public class HomeFragment extends Fragment {
    RecyclerView recyclerView;
    ArrayList<PrankListModel> prankListModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_view, container, false);
        return view;

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = view.findViewById(R.id.rlv_prank);

        ImmUtils.setStatusBar(getActivity(),false,false);

        try {
            prankListModel = JsonUtils.parseJsonListFromAssets(getActivity(),"prank.json");

            Constant.prankListModel.clear();
            Constant.prankListModel.addAll(prankListModel);

        } catch (Exception e) {
            e.printStackTrace();
        }

        PrankListAdapter prankListAdapter = new PrankListAdapter(R.layout.item_prank,prankListModel);

        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        recyclerView.setAdapter(prankListAdapter);
        prankListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(getActivity(), PrankDetailsActivity.class).putExtra("id",prankListModel.get(position).getCategoryId()));
            }
        });
    }

}
