package com.yw.fastviewlibrary.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import java.util.List;

/**
 * @author:heinigger 2019/4/23  16:50
 * @function:
 */
public class ActivityFragmentHelper {

    private AppCompatActivity activity;
    private int mContentId;
    public final static String WHERE = "where_frgment";
    private Fragment mFragment;
    private boolean shouldFinish;

    public ActivityFragmentHelper(AppCompatActivity activity) {
        this.activity = activity;
    }

    public void setContentId(int mContentId) {
        this.mContentId = mContentId;
    }

    /**
     * 获取传递过来的Fragment,并切换布局
     **/
    public void switchFragment() {
        Class clazz = (Class) activity.getIntent().getSerializableExtra(WHERE);
        if (clazz != null && mContentId != 0) {
            switchFragment(clazz);
        }
    }

    public Class getFragmentClass() {
        return (Class) activity.getIntent().getSerializableExtra(WHERE);
    }

    /**
     * 用来方便fagment的使用,fagment是一种简便的,activity代替品
     */
    public void switchFragment(Class<?> cls, Object... objects) {
        if (cls == null) {//如果为空,直接finish
            if (shouldFinish) {
                activity.finish();
            }
            return;
        }
        String mClassName = cls.getName();
        FragmentManager fm = activity.getSupportFragmentManager();
        //先将其他的fagment进行隐藏
        FragmentTransaction ft = fm.beginTransaction();
        List<Fragment> fragments = fm.getFragments();
        if (fragments != null && !fragments.isEmpty()) {
            for (int i = 0; i < fragments.size(); i++) {
                Fragment fragmentNow = fragments.get(i);
                if (fragmentNow != null && !mClassName.equals(fragmentNow.getTag()) && !fragmentNow.isHidden()) {
                    ft.hide(fragmentNow);
                }
            }
        }
        Fragment fragmentShow = fm.findFragmentByTag(mClassName);
        if (fragmentShow == null) {
            fragmentShow = Fragment.instantiate(activity, mClassName);
            try {
                Bundle bundle = activity.getIntent().getExtras();
                if (bundle == null) {
                    bundle = new Bundle();
                }
//                bundle.putSerializable(BaseFragment.OBJECTKEY, objects);
                fragmentShow.setArguments(bundle);
                ft.add(mContentId, fragmentShow, mClassName);
            } catch (Exception e) {
                throw new IllegalArgumentException("please set ContentID for Fragment");
            }
        } else {
            ft.show(fragmentShow);
        }
        mFragment = fragmentShow;
        ft.commitAllowingStateLoss();
    }


    public void switchFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        String mClassName = fragment.getClass().getName();
        FragmentManager fm = activity.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment currentFragment = fm.findFragmentByTag(fragment.getClass().getName());
        List<Fragment> fragments = fm.getFragments();
        if (fragments != null && !fragments.isEmpty()) {
            for (int i = 0; i < fragments.size(); i++) {
                Fragment fragmentNow = fragments.get(i);
                if (fragmentNow != null && !mClassName.equals(fragmentNow.getTag()) && !fragmentNow.isHidden()) {
                    ft.hide(fragmentNow);
                }
            }
        }
        if (currentFragment != null) {
            ft.show(fragment);
        } else {
            ft.add(mContentId, fragment, fragment.getClass().getName());
        }
        mFragment = currentFragment;
        ft.commitAllowingStateLoss();
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (mFragment != null) {
            mFragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void showDialogFragment(Class<?> clazz) {
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        String clsName = clazz.getName();
        List<Fragment> fragments = fragmentManager.getFragments();
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            if (fragment != null && fragment instanceof DialogFragment) {
                DialogFragment dialogFragment = (DialogFragment) fragment;
                if (dialogFragment.isVisible()) {
                    dialogFragment.dismiss();
                }
            }
        }
        Fragment fragmentShow = fragmentManager.findFragmentByTag(clsName);
        DialogFragment dialogFragment = null;
        if (fragmentShow != null && fragmentShow instanceof DialogFragment) {
            dialogFragment = (DialogFragment) fragmentShow;
        } else {
            dialogFragment = (DialogFragment) DialogFragment.instantiate(activity, clsName);
        }
        dialogFragment.show(activity.getSupportFragmentManager(), clsName);

    }


    public Fragment getCurrentFragment() {
        return mFragment;
    }

    public void setShouldFinish(boolean shouldFinish) {
        this.shouldFinish = shouldFinish;
    }
}
