package com.paint.trace.drawing.frag;

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.paint.trace.drawing.topon.AdManager;
import com.paint.trace.drawing.topon.onActionListener;
import com.paint.trace.drawing.view.MyApplication;
import com.paint.trace.drawing.view.PreViewActivity;
import com.paint.trace.drawing.view.CatogryActivity;
import com.paint.trace.drawing.item.HomeAdapter;
import com.paint.trace.drawing.databinding.FragmentMainBinding;
import com.paint.trace.drawing.alllistener.onItemListener;

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

    public HomeFragment() {

    }

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMainBinding.inflate(getLayoutInflater());
        onInitRecycler();

        return binding.getRoot();
    }

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


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


        if (viewAll) {
            AdManager.showTopOn(requireActivity(), new onActionListener() {
                @Override
                public void onAction() {
                    Intent intent = new Intent(requireContext(), CatogryActivity.class);
                    intent.putExtra(MyApplication.Key_ViewDir, dir);
                    startActivity(intent);
                }
            });
        } else {
            AdManager.showTopOn(requireActivity(), new onActionListener() {
                @Override
                public void onAction() {
                    Intent intent = new Intent(requireContext(), PreViewActivity.class);
                    intent.putExtra(MyApplication.Key_Camera, dir);
                    intent.putExtra(MyApplication.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;
//            }
//        });
    }
}