package com.tapjoy.easyapp;

import android.os.Bundle;
import android.util.Pair;
import android.webkit.WebView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

public class TapjoyEasyApp extends AppCompatActivity implements TabLayoutMediator.TabConfigurationStrategy {

    private TabLayout tabLayout;
    private final ArrayList<Pair<String, Fragment>> screens = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        ConstraintLayout container = findViewById(R.id.container);
        ViewCompat.setOnApplyWindowInsetsListener(container, (v, insets) -> {
            Insets bars = insets.getInsets(
                    WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout());
            v.setPadding(bars.left, bars.top, bars.right, bars.bottom);
            return WindowInsetsCompat.CONSUMED;
        });

        WebView.setWebContentsDebuggingEnabled(true);

        screens.add(new Pair<>(getString(R.string.tab_main), new MainFragment()));
        screens.add(new Pair<>(getString(R.string.tab_user), new UserFragment()));
        screens.add(new Pair<>(getString(R.string.tab_offerwall_discover), new OfferwallDiscoverFragment()));
        screens.add(new Pair<>(getString(R.string.tab_config), new ConfigurationFragment()));
        screens.add(new Pair<>(getString(R.string.tab_unity), new UnityAdsFragment()));

        tabLayout = findViewById(R.id.tabLayout);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                replaceFragment(tab.getPosition());
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {}
            @Override
            public void onTabReselected(TabLayout.Tab tab) {}
        });

        for (int i = 0; i < screens.size(); i++) {
            tabLayout.addTab(tabLayout.newTab().setText(screens.get(i).first));
        }
    }

    @Override
    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
        String title = screens.get(position).first;
        if(position == 2) {
            tab.setCustomView(R.layout.tab);
            TextView titleText = tab.getCustomView().findViewById(R.id.tabTitle);
            titleText.setText(title);
            titleText.setTextColor(tabLayout.getTabTextColors());
        } else {
            tab.setText(title);
        }
    }

    private void replaceFragment(int index) {
        Fragment fragment = screens.get(index).second;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(R.id.fragmentContainer, fragment);
        }
        // Hide the other fragments
        for (int i = 0; i < screens.size(); i++) {
            if (screens.get(i).second.isAdded() && i != index) {
                transaction.hide(screens.get(i).second);
            }
        }
        transaction.commitAllowingStateLoss();
    }

}
