package cn.yyxx.commsdk.platform.hnyy;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.util.LinkedList;



/**
 * @author #Suyghur.
 * Created on 2021/10/25
 */
public class BaseLoginActivity extends FragmentActivity {

    protected LinkedList<String> fragmentStack = new LinkedList<>();
    protected String currentFragmentTag = "";
    protected int containerId = 0;


    public void switchFragment(String tag, Bundle bundle) {

    }

    public void switchFragment(Fragment fragment, String tag, Bundle bundle) {

        if (fragmentStack == null) {
            return;
        }

        if (fragmentStack.contains(tag)) {
            //栈里最后一个与tag相同才执行删除
            if (fragmentStack.size() > 1 && !fragmentStack.getLast().equals(tag)) {
                fragmentStack.removeLast();
            }
        } else {
            fragmentStack.add(tag);
        }


        if (bundle !=null ){
            fragment.setArguments(bundle);
        }

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        Fragment tagFragment = getSupportFragmentManager().findFragmentByTag(currentFragmentTag);
        if (tagFragment != null && !tag.equals(currentFragmentTag)) {
            tagFragment.onPause();
            transaction.hide(tagFragment);
        }
        tagFragment = getSupportFragmentManager().findFragmentByTag(tag);
        if (tagFragment == null) {
            transaction.add(containerId, fragment, tag);
        } else {
            tagFragment.onResume();
            transaction.show(tagFragment);
        }


        currentFragmentTag = tag;
        transaction.commitAllowingStateLoss();
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    protected boolean isShouldHideInput(View v, MotionEvent event) {
        if ((v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            // 点击的是输入框区域，保留点击EditText的事件
            return !(event.getX() > left) || !(event.getX() < right) || !(event.getY() > top) || !(event.getY() < bottom);
        }
        return false;
    }

    protected void hideBar() {
        // The UI options currently enabled are represented by a bitfield.
        // getSystemUiVisibility() gives us that bitfield.
        int uiOptions = getWindow().getDecorView().getSystemUiVisibility();
        int newUiOptions = uiOptions;
        boolean isImmersiveModeEnabled =
                ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
        if (!isImmersiveModeEnabled) {
            if (Build.VERSION.SDK_INT >= 14) {
                newUiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
            }
            if (Build.VERSION.SDK_INT >= 16) {
                newUiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
            }
            if (Build.VERSION.SDK_INT >= 18) {
                newUiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            }
            getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
        }
    }

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