package com.draw.painting.test.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.LinearLayoutManager;

import com.draw.painting.test.PaintingApp;
import com.draw.painting.test.adapter.HomeAdapter;
import com.draw.painting.test.databinding.FragmentHomeBinding;
import com.draw.painting.test.onClickListener;

public class HomeFragment extends Fragment implements onClickListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";



    private FragmentHomeBinding fragmentHomeBinding;

    public HomeFragment() {

    }

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        fragmentHomeBinding = FragmentHomeBinding.inflate(getLayoutInflater());
        onInitRecycler();
        return fragmentHomeBinding.getRoot();
    }

    private void onInitRecycler() {
        HomeAdapter homeAdapter = new HomeAdapter(requireContext());
        homeAdapter.setListener(this::onClickItem);
        fragmentHomeBinding.recyclerview.setAdapter(homeAdapter);
        fragmentHomeBinding.recyclerview.setLayoutManager(new LinearLayoutManager(requireContext()));
    }



    @Override
    public void onClickItem(boolean viewAll,String dir) {

        Intent intent;
        if(viewAll){
            intent = new Intent(requireContext(), PreViewActivity.class);
            intent.putExtra(PaintingApp.Key_ViewDir, dir);
        }else {
            intent = new Intent(requireContext(), CameraActivity.class);
            intent.putExtra(PaintingApp.Key_Camera,dir);
            intent.putExtra(PaintingApp.Key_ISMAIN,true);
        }
        startActivity(intent);



//        String place;
//        if(viewAll){
//            place = IronSourceAd.adPlace2;
//        }else {
//            place = IronSourceAd.adPlace3;
//        }
//        IronSourceAd.showAd(requireActivity(), place, new Function1<Integer, Unit>() {
//            @Override
//            public Unit invoke(Integer integer) {
//                if (integer == IronSourceAd.SHOW_FAIL || integer == IronSourceAd.NO_CACHE || integer == IronSourceAd.SHOW_CLOSE){
//
//                    Intent intent;
//                    if(viewAll){
//                        intent = new Intent(requireContext(), PreViewActivity.class);
//                        intent.putExtra(PaintingApp.Key_ViewDir, dir);
//                    }else {
//                        intent = new Intent(requireContext(), CameraActivity.class);
//                        intent.putExtra(PaintingApp.Key_Camera,dir);
//                    }
//                    startActivity(intent);
//
//                }
//
//                return null;
//            }
//        });
    }
}