package com.apps.chillscreen.uiView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.apps.chillscreen.R;
import com.apps.chillscreen.databinding.ActivityBaseBinding;

public class BaseActivity extends AppCompatActivity {
    ActivityBaseBinding binding;
    private boolean isFragmentTransitionRunning = false;
    private long lastClickTime = 0;
    private static final long CLICK_THROTTLE = 300; // 300毫秒内不处理重复点击

    // 缓存Fragment实例
    private Fragment homeFragment;
    private Fragment collectFragment;
    private Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBaseBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initFragments();
        init();
        if (savedInstanceState == null) {
            showInitialFragment();
        }
    }
    private void showInitialFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(binding.fragmentIn.getId(), homeFragment);
        currentFragment = homeFragment;
        transaction.commit();

        // 更新UI状态
        updateTab(0);
    }
    private void initFragments() {
        homeFragment = new DiscoverFragment();
        collectFragment = new BookmarkShowFragment();
        currentFragment = homeFragment;
    }

    private void init() {
        // 初始加载HomeFragment
        switchFragment(0);

        binding.llHome.setOnClickListener(v -> {
            if (System.currentTimeMillis() - lastClickTime > CLICK_THROTTLE) {
                switchFragment(0);
                lastClickTime = System.currentTimeMillis();
            }
        });

        binding.llLike.setOnClickListener(v -> {
            if (System.currentTimeMillis() - lastClickTime > CLICK_THROTTLE) {
                switchFragment(1);
                lastClickTime = System.currentTimeMillis();
            }
        });
        binding.llBottom.setOnClickListener(v -> {});
        binding.imgSearch.setOnClickListener(v -> {
            Intent intent=new Intent(this,ExploreActivity.class);
            startActivity(intent);
        });
    }

    private void switchFragment(int position) {
        if (isFragmentTransitionRunning) {
            return;
        }

        Fragment targetFragment = null;
        switch (position) {
            case 0:
                if (currentFragment instanceof DiscoverFragment) return;
                targetFragment = homeFragment;
                break;
            case 1:
                if (currentFragment instanceof BookmarkShowFragment) return;
                targetFragment = collectFragment;
                break;
        }

        if (targetFragment == null || binding.fragmentIn.getId() == View.NO_ID) {
            return;
        }

        updateTab(position);
        isFragmentTransitionRunning = true;

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.setCustomAnimations(
                android.R.anim.fade_in,
                android.R.anim.fade_out
        );

        // 检查Fragment是否已经添加
        if (targetFragment.isAdded()) {
            transaction.show(targetFragment);
        } else {
            transaction.add(binding.fragmentIn.getId(), targetFragment);
        }

        // 隐藏当前Fragment
        if (currentFragment != null && currentFragment.isAdded()) {
            transaction.hide(currentFragment);
        }

        currentFragment = targetFragment;

        transaction.runOnCommit(() -> isFragmentTransitionRunning = false);
        transaction.commit();
    }

    private void updateTab(int position) {
        binding.imgHome.setSelected(false);
        binding.imgLike.setSelected(false);
        binding.tvStart.setTextColor(getResources().getColor(R.color.no_select, getTheme()));
        binding.tvEnd.setTextColor(getResources().getColor(R.color.no_select, getTheme()));

        switch (position) {
            case 0:
                binding.imgHome.setSelected(true);
                binding.tvStart.setTextColor(getResources().getColor(R.color.title, getTheme()));
                break;
            case 1:
                binding.imgLike.setSelected(true);
                binding.tvEnd.setTextColor(getResources().getColor(R.color.title, getTheme()));
                break;
        }
    }
}