package hf.lskj.com.carstockcount.activity;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import org.greenrobot.eventbus.Subscribe;

import hf.lskj.com.carstockcount.event.ExitEvent;
import hf.lskj.com.carstockcount.utils.KeyBoardUtils;
import hf.lskj.com.carstockcount.utils.LogUtils;

/*
Activity公共基类
封装ActionBar的常用方法
提供ProgressDialog
 */
public class BaseActivity extends AppCompatActivity {
    protected ProgressDialog sPd;
    protected final String TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.onCreate(savedInstanceState);

        sPd = new ProgressDialog(this);
        sPd.setCanceledOnTouchOutside(false);
        KeyBoardUtils.closeKeybord(this);

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    protected static void startActivity(Context context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        setTitle(title, true);
    }

    protected void setTitle(CharSequence title, boolean displayHomeAsUp) {
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(title);
            getSupportActionBar().setDisplayHomeAsUpEnabled(displayHomeAsUp);
            getSupportActionBar().setDisplayShowHomeEnabled(displayHomeAsUp);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        /*
          设置为竖屏
         */
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        LogUtils.i("className", getClass().getName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        KeyBoardUtils.closeKeybord(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void showPd(String msg) {
        if (sPd == null) {
            return;
        }
        if (sPd.isShowing()) {
            return;
        }
        sPd.setMessage(msg);
        sPd.show();
    }

    public void showPd() {
        showPd("请稍候...");
    }

    public void cancelPd() {
        if (sPd == null) {
            return;
        }
        sPd.cancel();
    }

    @Subscribe()
    public void onMessageEvent(ExitEvent event) {
        finish();
    }

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

                InputMethodManager imm = (InputMethodManager) 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);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (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();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
}
