package com.wall.snap.wallpaper.acctivitysnap;
import android.graphics.Bitmap;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.google.android.material.tabs.TabLayout;
import com.tradplus.ads.base.bean.TPAdInfo;
import com.wall.snap.wallpaper.R;
import com.wall.snap.wallpaper.ad.AdsInsUtil;
import com.wall.snap.wallpaper.ad.LoadListener;
import com.wall.snap.wallpaper.databinding.ActivityMainsnapBinding;
import com.wall.snap.wallpaper.databinding.ItemTabsnapBinding;
import com.wall.snap.wallpaper.toolsnap.ComUtilssnap;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.blurry.Blurry;


public class MainsnapActivity extends AppCompatActivity {

    private ImageView imageView;

    private ActivityMainsnapBinding vb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityMainsnapBinding.inflate(getLayoutInflater());
        ComUtilssnap.initFull(this, false);
        setContentView(vb.getRoot());
        loadAd();
        List<Fragment> list = new ArrayList<>();
        list.add(com.wall.snap.wallpaper.fragmentsnap.FragmentMainsnap.newInstance(0));
//        list.add(FragUiSearch.newInstance());
        list.add(com.wall.snap.wallpaper.fragmentsnap.FragmentLikesnap.newInstance());
//        list.add(FragmentDownload.newInstance());

        int[] ints = {R.drawable.selector_home2, R.drawable.selector_tab_like2};
        vb.viewpager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return list.get(position);
            }

            @Override
            public int getItemCount() {
                return list.size();
            }
        });
        vb.viewpager2.setUserInputEnabled(false);

        for (int i = 0; i < list.size(); i++) {
            TabLayout.Tab tab = vb.tab.newTab();
            ItemTabsnapBinding tabVb = ItemTabsnapBinding.inflate(getLayoutInflater());
            tabVb.imageIcon.setImageDrawable(ContextCompat.getDrawable(this, ints[i]));
            tab.setCustomView(tabVb.getRoot());
//            if (i == 0) {
//                tabVb.imageIndicator.setVisibility(View.VISIBLE);
//            } else {
//                tabVb.imageIndicator.setVisibility(View.GONE);
//            }
            vb.tab.addTab(tab);
        }

        vb.tab.setTabTextColors(Color.TRANSPARENT, Color.TRANSPARENT);
        vb.tab.setSelectedTabIndicatorHeight(0);

        vb.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                vb.viewpager2.setCurrentItem(position);
                View customView = tab.getCustomView();
//                View imageIndicator = customView.findViewById(R.id.image_indicator);
//                imageIndicator.setVisibility(View.VISIBLE);
                if (position == 0) {
                    vb.textviewWallpaper.setText(getString(R.string.app_name));
                } else {
                    vb.textviewWallpaper.setText(getString(R.string.collections));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
//                View imageIndicator = customView.findViewById(R.id.image_indicator);
//                imageIndicator.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

//        vb.imSearch.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(MainsnapActivity.this, SearchsnapActivity.class);
//                startActivity(intent);
//            }
//        });
//
        vb.searchBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainsnapActivity.this, SearchsnapActivity.class);
                startActivity(intent);
            }
        });


        imageView = findViewById(R.id.blurred_background);

        // 通过加载高清图片来进行模糊处理
        Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.beijing);

        // 使用 Blurry 库模糊图片
        Blurry.with(this)
                .radius(50)  // 模糊半径，数值越大模糊越强
                .sampling(20) // 图片的采样率，越大模糊效果越明显
                .from(originalBitmap)  // 原始图片
                .into(imageView);  // 结果显示到 imageView


    }

    private void loadAd() {
        AdsInsUtil.INSTANCE.loadAd(this, AdsInsUtil.Placement.TOP_ON_AD_THREE, new LoadListener() {
            @Override
            public void loaded(@NotNull TPAdInfo ad) {

            }

            @Override
            public void loadFailed(@NotNull String error) {

            }
        });
        AdsInsUtil.INSTANCE.loadAd(this, AdsInsUtil.Placement.TOP_ON_AD_TOW, new LoadListener() {
            @Override
            public void loaded(@NotNull TPAdInfo ad) {

            }

            @Override
            public void loadFailed(@NotNull String error) {

            }
        });
    }
}