package com.exquisite.activity;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;


import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.exquisite.KeyboardSkin;
import com.exquisite.callback.Adcallback;
import com.exquisite.fragment.AllFragment;
import com.exquisite.R;

import com.exquisite.tools.Mytools;

import java.util.List;

public class AllActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout back;
    private List<ATInterstitial> ads;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.allactivity), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        ads = KeyboardSkin.lodAd();

        AllFragment fragment = new AllFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.all_activity_fragment, fragment);
        transaction.commit();
        intView();
        intEvent();
    }




    private void intEvent() {
        back.setOnClickListener(this);
    }

    private void intView() {
        back = findViewById(R.id.back);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            showAd();

        }
    }

    private void showAd() {
        ATInterstitial mInterstitialAd = Mytools.onCache(ads);
        if (mInterstitialAd == null) {
            finish();
        } else {
            if (mInterstitialAd.isAdReady()) {
                Mytools.setCallback(mInterstitialAd, new Adcallback() {
                    @Override
                    public void onShowFail(AdError ad) {
                        finish();
                    }

                    @Override
                    public void onAdHidden() {
                        finish();
                    }
                });
                mInterstitialAd.show(this);
            } else {
                mInterstitialAd.load();
            }
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        showAd();
    }
}