package com.trace.ar.drawing.fragment;

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.trace.ar.drawing.ARApp;
import com.trace.ar.drawing.activity.DetailActivity;
import com.trace.ar.drawing.activity.GalleryActivity;
import com.trace.ar.drawing.activity.ViewAllActivity;
import com.trace.ar.drawing.adapter.HomeFragmentAdapter;
import com.trace.ar.drawing.databinding.FragmentMainBinding;
import com.trace.ar.drawing.listener.onClickListener;

public class HomeFragment extends Fragment implements onClickListener {
    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();
        initClick();
        return binding.getRoot();
    }

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



    private void initClick(){
        binding.bannerGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(requireContext(), GalleryActivity.class);
//                intent.putExtra(ARApp.Key_Camera, name);
                startActivity(intent);
            }
        });

        binding.bannerCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(requireContext(), DetailActivity.class);
//                intent.putExtra(ARApp.Key_Camera, name);
               startActivity(intent);
            }
        });
    }

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

        Intent intent;
        if(viewAll){
            intent = new Intent(requireContext(), ViewAllActivity.class);
            intent.putExtra(ARApp.Key_ViewDir, dir);
        }else {
            intent = new Intent(requireContext(), DetailActivity.class);
            intent.putExtra(ARApp.Key_Camera,dir);
            intent.putExtra(ARApp.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;
//            }
//        });
    }
}