package com.fun.originalityprank.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.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.viewpager.widget.ViewPager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import com.fun.originalityprank.PrankSkin;
import com.fun.originalityprank.R;
import com.fun.originalityprank.adapter.PagerAdapter;
import com.fun.originalityprank.databinding.ActivityMainBinding;
import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;
    private BroadcastReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.activity_main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(PrankSkin.AD_BROADCAST)) {
                    Log.e("zzj", "dawlkdawldkhalwkdhalwjdhawl");
                } else {
                    Log.e("zzj", "               111               ");
                }
            }
        }, new IntentFilter(PrankSkin.AD_BROADCAST));


        initView();
        setTab(0);

    }

    private void initView() {

        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        binding.mainPager.setAdapter(pagerAdapter);
        binding.mainTab.setupWithViewPager(binding.mainPager);
        binding.mainPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTab(position);
                LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(new Intent(PrankSkin.AD_BROADCAST));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setTab(int position) {
        for (int i = 0; i < binding.mainTab.getTabCount(); i++) {
            final TabLayout.Tab tab = binding.mainTab.getTabAt(i);
            if (tab != null) {
                switch (i) {
                    case 0:
                        tab.setIcon(position == 0 ? R.drawable.main_check : R.drawable.main_uncheck);
                        break;
                    case 1:
                        tab.setIcon(position == 1 ? R.drawable.like_check : R.drawable.like_uncheck);
                        break;
                    default:
                        break;
                }
            }
        }
    }

}