package com.zztrapp.zztrapp.ui.currency.activity;

import android.view.View;

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

import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.databinding.ActivitySpecialTasksBinding;
import com.zztrapp.zztrapp.ui.currency.fragment.BlackWaterFragment;
import com.zztrapp.zztrapp.ui.currency.fragment.SewageFacilitiesFragment;
import com.zztrapp.zztrapp.utilcode.util.AdaptScreenUtils;
import com.zztrapp.zztrapp.utilcode.util.ConvertUtils;

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

/**
 * Describe 专项任务
 * Created by Bby on 2022/6/23
 * Email bby0856@163.com
 */
public class SpecialTasksActivity extends BaseActivity {

    private ActivitySpecialTasksBinding binding;

    private List<Fragment> fragments;
    private int currentIndex = 0;
    private boolean isFirstEnter = true;//是否为第一次进入该界面,如果不是第一次进入该界面


    @Override
    public void onResume() {
        super.onResume();
//        if (!isFirstEnter) {
//            EventBus.getDefault().post(new EventBusMessageBean("QcTaskFragment"));
//        }
        isFirstEnter = false;
    }

    @Override
    public View getLayoutId() {
        return binding.getRoot();
    }

    @Override
    protected void initBinding() {
        binding = ActivitySpecialTasksBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {
        binding.navView.navTitle.setText("专项任务");
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
//        binding.nav1Name.getPaint().setFakeBoldText(true);
        currentIndex = 0;
        fragments = new ArrayList<>();
        fragments.add(new BlackWaterFragment());
        fragments.add(new SewageFacilitiesFragment());
        //默认加载第一个Fragment对象
        getSupportFragmentManager().beginTransaction().add(binding.fl.getId(), fragments.get(0)).commit();
    }

    @Override
    protected void initOnClick() {
        binding.nav1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                binding.nav1Name.getPaint().setFakeBoldText(true);
//                binding.nav2Name.getPaint().setFakeBoldText(false);
                binding.nav1Name.setTextSize(ConvertUtils.px2sp(AdaptScreenUtils.pt2Px(18)));
                binding.nav2Name.setTextSize(ConvertUtils.px2sp(AdaptScreenUtils.pt2Px(16)));
                binding.nav1Name.setTextColor(getResources().getColor(R.color.colorFFFFFFe6));
                binding.nav2Name.setTextColor(getResources().getColor(R.color.colorFFFFFFbf));
                switchFragment(0);
            }
        });
        binding.nav2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                binding.nav1Name.getPaint().setFakeBoldText(false);
//                binding.nav2Name.getPaint().setFakeBoldText(true);
                binding.nav1Name.setTextSize(ConvertUtils.px2sp(AdaptScreenUtils.pt2Px(16)));
                binding.nav2Name.setTextSize(ConvertUtils.px2sp(AdaptScreenUtils.pt2Px(18)));
                binding.nav1Name.setTextColor(getResources().getColor(R.color.colorFFFFFFbf));
                binding.nav2Name.setTextColor(getResources().getColor(R.color.colorFFFFFFe6));
                switchFragment(1);
            }
        });
    }

    /**
     * 切换Fragment的方法
     *
     * @param targetIndex
     */
    private void switchFragment(int targetIndex) {
        //目标的Fragment
        Fragment targetFragment = fragments.get(targetIndex);
        //当前的Fragment
        Fragment currentFragment = fragments.get(currentIndex);
//        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (targetFragment.isAdded()) {
            //目标Fragment已经被添加了...则隐藏当前正在展示的Fragment,显示目标Fragment
            fragmentTransaction.hide(currentFragment).show(targetFragment);
        } else {
            //没有被添加...则隐藏当前的Fragment,添加目标Fragment
            fragmentTransaction.hide(currentFragment).add(binding.fl.getId(), targetFragment);
        }
        fragmentTransaction.commit();
        //改变当前索引值
        currentIndex = targetIndex;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}