package com.ar.drawing.ui;

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

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.ar.drawing.MyDraw;
import com.ar.drawing.adapter.MainAdapter;
import com.ar.drawing.api.OnClickListener;
import com.ar.drawing.databinding.FragmentHomeBinding;

public class Home extends Fragment implements OnClickListener {

    private FragmentHomeBinding binding;

    public Home() {

    }
    public static Home newInstance() {
        Home fragment = new Home();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(getLayoutInflater());
        onInitRecycler();
        binding.btnImport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HomeActivity fragmentActivity1 = (HomeActivity) requireActivity();
                fragmentActivity1.checkPermission();
            }
        });

        return binding.getRoot();
    }

    private void onInitRecycler() {
        MainAdapter mainAdapter = new MainAdapter(requireContext());
        mainAdapter.setListener(this::onClickItem);
        binding.recyclerview.setAdapter(mainAdapter);
        binding.recyclerview.setLayoutManager(new GridLayoutManager(requireContext(), 1));
    }


    @Override
    public void onClickItem(String dir) {
        Intent intent = new Intent(requireContext(), ItemActivity.class);
        intent.putExtra(MyDraw.Key_ViewDir, dir);
        startActivity(intent);

    }
}