package com.mq.mylibrary.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.TextView;

import com.mq.mylibrary.R;
import com.mq.mylibrary.dialog.DateTimeDialog;
import com.mq.mylibrary.dialog.MyProgressDialog;
import com.mq.mylibrary.dialog.StartEndDateDialog;
import com.mq.mylibrary.popupwindow.ListPopupwindow;
import com.mq.mylibrary.popupwindow.SpinnerOption;
import com.mq.mylibrary.popupwindow.adapter.SelAdapter;
import com.mq.mylibrary.tool.ETTool;
import com.mq.mylibrary.tool.LogTool;

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

/**
 * 创建人 mQ
 * 创建时间 2019/8/6 0006 19:56
 * 文件名称 BaseActivity
 * 说明:  Activity基类
 **/
public class BaseActivity extends AppCompatActivity {

    private MyProgressDialog mProgressDialog;
    private DateTimeDialog mDateTimeDialog;
    private ListPopupwindow mListPopupwindow;
    private SelAdapter mListAdapter;
    private StartEndDateDialog mStartEndDateDialog;

    private ArrayList<MyOnTouchListener> onTouchListeners = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogTool.i("onCreate");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogTool.i("onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogTool.i("onPause");
    }

    public void showPd(String msg) {
        if (mProgressDialog == null)
            mProgressDialog = new MyProgressDialog(this);
        mProgressDialog.setMessage("请稍后...");
        if (msg != null)
            mProgressDialog.setMessage(msg);
        mProgressDialog.show();
    }

    public void showPd() {
        showPd(null);
    }

    public void cancelPd() {
        if (mProgressDialog != null)
            mProgressDialog.dismiss();
    }


    public void showDateTimeDialog(View view, String dateTime) {
        if (mDateTimeDialog == null) {
            mDateTimeDialog = new DateTimeDialog(this);
        }
        mDateTimeDialog.setTime(view,dateTime);
        mDateTimeDialog.setCallBack(new DateTimeDialog.DateTimeCallBack() {
            @Override
            public void onDateTimeResultListener(View view, String date, String time) {
                ETTool.setData(view, date + " " + time);
            }
        });
        mDateTimeDialog.show();
    }


    public void showListPopupwindow(View view, List<SpinnerOption> list, SelAdapter.SelectListener mSelectListener) {
        if (mListPopupwindow == null) {
            mListAdapter = new SelAdapter(null);
            mListAdapter.setHideSelView();
            mListAdapter.setSelectListener(mSelectListener);
            mListPopupwindow = new ListPopupwindow(this, 1, mListAdapter);
//            mListPopupwindow.setWidthMatch();
        }
        mListAdapter.replaceData(list);
        mListAdapter.setView(view);
        mListPopupwindow.show(view);
    }

    public void getSelPopupwindow(View view, Set<SpinnerOption> selectList) {
        mListPopupwindow.dismiss();
        SpinnerOption option = selectList.iterator().next();
        if (view instanceof TextView) {
            TextView textView = (TextView) view;
            textView.setText(option.name);
            textView.setError(null);
        }
    }

    public Context getContext() {
        return this;
    }

    public void showStartEndDateDialog(StartEndDateDialog.DateCallBack callBack) {
        if (mStartEndDateDialog == null) {
            mStartEndDateDialog = new StartEndDateDialog(getContext());
            mStartEndDateDialog.setCallBack(callBack);
        }
        mStartEndDateDialog.show();
    }

    public void dismissStartEndDateDialog() {
        if (mStartEndDateDialog != null) {
            mStartEndDateDialog.dismiss();
        }
    }

    /*点击屏幕非文本框的区域,关闭键盘*/
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        for (MyOnTouchListener l : onTouchListeners) {
            if (l != null)
                l.onTouch(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 void registOnTouchListener(MyOnTouchListener listener) {
        onTouchListeners.add(listener);
    }

    /*** 解除触摸事件 ***/
    public void unregistOnTouchListener(MyOnTouchListener listener) {
        onTouchListeners.remove(listener);
    }

    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();
            return !(event.getRawX() > left && event.getRawX() < right
                    && event.getRawY() > top && event.getRawY() < bottom);
        }
        return false;
    }


    /**
     * 设置下拉选择输入框
     */
    public void setAutoTextView(AutoCompleteTextView textView, List<String> list) {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, list);
        textView.setAdapter(adapter);
        textView.setThreshold(0);
        textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {//获取焦点时
                    textView.showDropDown();
                }
            }
        });
    }

}
