package com.swiftcleaner.chovey.view.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.business.admob.AdmobManager;
import com.swiftcleaner.chovey.view.MainActivity;
import com.swiftcleaner.chovey.view.activity.GuideActivity;

import io.reactivex.rxjava3.annotations.Nullable;

public class GuideThreeFragment extends Fragment {

    private AppCompatButton buttonNext;
    private TextView skipId;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_guide_three, container, false);
        buttonNext = view.findViewById(R.id.three_button);
        skipId = view.findViewById(R.id.skip_id);

        buttonNext.setOnClickListener(v -> {
            // 通知Activity切换到下一个页面
            if (getActivity() instanceof GuideActivity) {
                ((GuideActivity) getActivity()).nextPage();
            }
        });

        skipId.setOnClickListener(v -> {
            if (AdmobManager.INSTANCE.isShowAdInterGuide()){
                AdmobManager.INSTANCE.showInterstitialAd(requireActivity(),
                        () -> {
                            startIntent();
                            Log.d("isShowAdInterGuide","Ad loading failed");
                            return null;
                        },
                        () -> {
                            Log.d("isShowAdInterGuide","Advertisement loaded successfully");
                            return null;
                        },
                        () -> {
                            Log.d("isShowAdInterGuide","Ad loading ends");
                            startIntent();
                            return null;
                        }, false);
            }else {
                startIntent();
                Log.d("isShowAdInterGuide",AdmobManager.INSTANCE.isShowAdInterGuide()+"Close Advertisement");
            }
        });

        return view;
    }
    private void startIntent(){
        startActivity(new Intent(requireActivity(), MainActivity.class));
        requireActivity().finish();
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        buttonNext = null;
        skipId = null;
    }
}