package com.rdiot.yx485.base;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.rdiot.yx485.BaseActivity;
import com.rdiot.yx485.R;
import com.rdiot.yx485.databinding.ActivityBaseBinding;

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

/**
 * @author fanbaozhu
 * @date 2020/12/10
 */
public abstract class BaseFragmentActivity extends BaseActivity implements View.OnClickListener {
    protected ActivityBaseBinding mBinding;
    protected FragmentManager mFragmentManager;
    protected Fragment mCurrentFragment;
    private List<Fragment> mHistoryList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityBaseBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());

        mBinding.llTitleBar.btnTitleLeft.setOnClickListener(this);
        mBinding.llTitleBar.btnTitleRight.setOnClickListener(this);
        mFragmentManager = getSupportFragmentManager();
        mHistoryList = new ArrayList<>();

        initTitleBar();
        initView();
        initData();
    }

    /**
     * 初始化标题
     */
    public abstract void initTitleBar();

    /**
     * 初始化界面
     */
    public abstract void initView();

    /**
     * 初始化数据
     */
    public void initData() {
    }

    public void onRightTextClick() {
    }

    @Override
    public void onClick(View v) {
        if (v == mBinding.llTitleBar.btnTitleLeft) {
            onBackPressed();
            return;
        }

        if (v == mBinding.llTitleBar.btnTitleRight) {
            onRightTextClick();
        }
    }

    public void addFragment(Fragment fragment) {
        if (fragment == null || mHistoryList.contains(fragment)) {
            return;
        }

        mHistoryList.add(fragment);
        mFragmentManager.beginTransaction().add(R.id.fl_container, fragment).commitNow();
    }

    public void changeFragment(Fragment fragment) {
        if (fragment == null || mHistoryList.contains(fragment)) {
            return;
        }

        if (mCurrentFragment == null) {
            mFragmentManager.beginTransaction().add(R.id.fl_container, fragment).commitNow();
            mCurrentFragment = fragment;
            return;
        }

        mFragmentManager.beginTransaction().add(R.id.fl_container, fragment).hide(mCurrentFragment).commitNow();
        mHistoryList.add(mCurrentFragment);
        mCurrentFragment = fragment;
    }

    public void goBack(String title) {
        if (!TextUtils.isEmpty(title)) {
            mBinding.llTitleBar.tvTitle.setText(title);
        }

        int count = mHistoryList.size();
        if (count > 0) {
            Fragment fragment = mHistoryList.get(count - 1);
            mHistoryList.remove(fragment);
            mFragmentManager.beginTransaction().remove(mCurrentFragment).show(fragment).commitNow();
            mCurrentFragment = fragment;
            mCurrentFragment.onResume();
            return;
        }

        finish();
    }

    public void removeFragment(Fragment fragment) {
        if (fragment == null || !mHistoryList.contains(fragment)) {
            return;
        }

        mFragmentManager.beginTransaction().remove(fragment).commitNowAllowingStateLoss();
        mHistoryList.remove(fragment);
    }

    @Override
    public void onBackPressed() {
        int count = mHistoryList.size();
        if (count > 0) {
            Fragment fragment = mHistoryList.get(count - 1);
            mHistoryList.remove(fragment);
            mFragmentManager.beginTransaction().remove(mCurrentFragment).show(fragment).commitNow();
            mCurrentFragment = fragment;
            mCurrentFragment.onResume();
            return;
        }
        super.onBackPressed();
    }
}
