package com.team.app.activities;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.team.app.Constant;
import com.team.app.Functions;
import com.team.app.R;
import com.team.app.controllers.GamePagerController;
import com.team.app.models.Cricket;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.AdapterStatus;
import com.google.android.material.tabs.TabLayout;

import java.util.Map;

public class GameDetailActivity extends AppCompatActivity {

    GamePagerController pagerController;
    TabLayout tabLayout;
    ViewPager viewPager;
    AdView adView0;
    public static Cricket cricket;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_game);
        MobileAds.initialize(this, initializationStatus -> {
            Map<String, AdapterStatus> statusMap = initializationStatus.getAdapterStatusMap();
            for (String adapterClass : statusMap.keySet()) {
                AdapterStatus status = statusMap.get(adapterClass);
            }
            FrameLayout adContainerView0 = findViewById(R.id.bannerAdView);
            if(Constant.adShown && !Constant.BANNER_ADS_ID.equals("")) {
                adView0 = new com.google.android.gms.ads.AdView(this);
                adView0.setAdUnitId(Constant.BANNER_ADS_ID);
                adContainerView0.addView(adView0);
                adView0.setAdSize(Functions.getAdSize(this));
                adView0.loadAd(new AdRequest.Builder().build());
                adContainerView0.setVisibility(View.VISIBLE);
            }
        });
        findViewById(R.id.back).setOnClickListener(view -> GameDetailActivity.this.onBackPressed());
        findViewById(R.id.gifImage).setOnClickListener(view -> startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse(getResources().getString(R.string.fantasy_power_11_link)))));
        tabLayoutSetup();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(adView0 != null) {
            adView0.destroy();
        }
    }

    private void tabLayoutSetup() {
        this.tabLayout = findViewById(R.id.tabLayout);
        this.viewPager = findViewById(R.id.viewPager);
        GamePagerController gamePagerController = new GamePagerController(getSupportFragmentManager(),
                FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,tabLayout.getTabCount());
        this.pagerController = gamePagerController;
        this.viewPager.setAdapter(gamePagerController);
        this.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }
        });
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    }
}
