package com.draw.painting.canvas.presentation;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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

import com.draw.painting.canvas.ad.AdsInsUtil;
import com.draw.painting.canvas.ad.LoadListener;
import com.draw.painting.canvas.core.SystemHelper;
import com.google.android.material.tabs.TabLayout;
import com.draw.painting.canvas.R;
import com.draw.painting.canvas.databinding.ActivityDashboardBinding;
import com.tradplus.ads.base.bean.TPAdInfo;

import org.jetbrains.annotations.NotNull;

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

public class DashboardActivity extends AppCompatActivity {

    private ActivityDashboardBinding binding;

    private List<Fragment> listFragment = new ArrayList<>();

    private String[] permissions;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 启用 EdgeToEdge 全屏显示
        SystemHelper.enableEdgeToEdge(this);
        
        binding = ActivityDashboardBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        
        listFragment.add(ExploreFragment.newInstance());
        listFragment.add(PreferencesFragment.newInstance());
        // 设置 WindowInsets 监听器
        setupInsets();
        
        onTabInit();
        binding.viewPager2.setUserInputEnabled(false);
        binding.viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return listFragment.get(position);
            }

            @Override
            public int getItemCount() {
                return listFragment.size();
            }
        });
        bingTabVp();

    }

    private void setupInsets() {
        // 设置根视图的 WindowInsets（处理顶部状态栏）
        SystemHelper.setupWindowInsetsListener(binding.getRoot());
        
        // 为底部导航栏添加底部系统栏间距
        SystemHelper.setupBottomNavigationInsets(binding.tab);
    }
    
    private void onInitPermission(){
        permissions = new String[]{android.Manifest.permission.CAMERA};
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            permissions = new String[]{android.Manifest.permission.CAMERA, android.Manifest.permission.READ_MEDIA_IMAGES};
        } else {
            permissions = new String[]{android.Manifest.permission.CAMERA, android.Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        }
        if (!SystemHelper.checkPermission(this,permissions)) {
            ActivityCompat.requestPermissions(this, permissions, 0);
        }else {
            Toast.makeText(this,getString(R.string.permission_fail),Toast.LENGTH_SHORT).show();
        }
    }

    private void bingTabVp() {
        binding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                ImageView icon = Objects.requireNonNull(tab.getCustomView()).findViewById(R.id.im_icon);
                TextView textView = Objects.requireNonNull(tab.getCustomView()).findViewById(R.id.textView);
                icon.setSelected(true);
                // 设置选中状态的珊瑚橙色
                icon.setColorFilter(ContextCompat.getColor(DashboardActivity.this, R.color.accent_color));
                textView.setTextColor(ContextCompat.getColor(DashboardActivity.this, R.color.accent_color));
                binding.viewPager2.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ImageView icon = Objects.requireNonNull(tab.getCustomView()).findViewById(R.id.im_icon);
                TextView textView = Objects.requireNonNull(tab.getCustomView()).findViewById(R.id.textView);
                icon.setSelected(false);
                // 设置未选中状态的灰色
                icon.setColorFilter(ContextCompat.getColor(DashboardActivity.this, R.color.text_secondary));
                textView.setTextColor(ContextCompat.getColor(DashboardActivity.this, R.color.text_secondary));
            }

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

            }
        });
        binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Objects.requireNonNull(binding.tab.getTabAt(position)).select();
            }
        });
    }


    private void onTabInit() {

        for (int i = 0; i < listFragment.size(); i++) {
            TabLayout.Tab tab = binding.tab.newTab();

            View inflate = LayoutInflater.from(this).inflate(R.layout.layout_tab_indicator, null, false);
            ImageView icon = inflate.findViewById(R.id.im_icon);
            TextView textView = inflate.findViewById(R.id.textView);
            if (i == 0) {
                textView.setText(getString(R.string.tab_draw));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_home));
                // 第一个tab默认选中，设置珊瑚橙色
                icon.setColorFilter(ContextCompat.getColor(this, R.color.accent_color));
                textView.setTextColor(ContextCompat.getColor(this, R.color.accent_color));
            } else {
                textView.setText(getString(R.string.tab_set));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_setting));
                // 未选中tab设置灰色
                icon.setColorFilter(ContextCompat.getColor(this, R.color.text_secondary));
                textView.setTextColor(ContextCompat.getColor(this, R.color.text_secondary));
            }
            tab.setCustomView(inflate);
            binding.tab.addTab(tab);
        }

    }




    @Override
    protected void onResume() {
        super.onResume();
        loadAd();
    }

    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) {

            }
        });
    }
}