package com.sjkj.base_lib.view;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.LifecycleObserver;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.sjkj.base_lib.R;
import com.sjkj.base_lib.app.AppManager;
import com.sjkj.base_lib.utils.AppTools;
import com.sjkj.base_lib.weight.LoadingDialog;


public abstract class BaseActivity extends AppCompatActivity implements BaseCallBack, LifecycleObserver {
    /**
     * activity堆栈管理
     */
    protected AppManager appManager = AppManager.getAppManager();

    private TextView title;
    private TextView right;
    private TitleListener titleListener;
    private TextView back;
    private int layoutId = 0;
    private View mActionBarView;
    public boolean isLogin = false;

    public void setTitleListener(TitleListener titleListener) {
        this.titleListener = titleListener;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 禁用横屏
        setContentView(initLayout());
        // 添加activity 到栈
        appManager.addActivity(this);
        initBinding();
        initBindingView(savedInstanceState);
        initActionBar();
        initView();
        initListener();
        loadData();
    }

    public void initBindingView(Bundle savedInstanceState) {

    }


    //布局
    public TextView getTitleView() {
        return title;
    }

    public TextView getBackView() {
        return back;
    }

    public TextView getRightView() {
        return right;
    }

    public View getTitleLayoutView() {
        return mActionBarView;
    }

    private View.OnClickListener backClick() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (titleListener != null) {
                    titleListener.onBackClick();
                }
                finish();
            }
        };
    }

    private View.OnClickListener rightClick() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (titleListener != null)
                    titleListener.onRightClick();
            }
        };
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 从栈中移除activity
        appManager.finishActivity(this);
    }


    @Override
    public void initBinding() {

    }

    /**
     * 绑定当前页面
     *
     * @return
     */
    public ViewDataBinding getViewDataBinding() {
        return DataBindingUtil.setContentView(this, initLayout());
    }

    @Override
    public void initActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
            mActionBarView = LayoutInflater.from(this).inflate(R.layout.title_layout, null);
            back = mActionBarView.findViewById(R.id.title_back);
            title = mActionBarView.findViewById(R.id.title_name);
            right = mActionBarView.findViewById(R.id.title_right);
            title.setText("");
            if (AppTools.isGhb()) {
                title.setTextSize(24);
                right.setTextSize(18);
            }
            back.setVisibility(View.VISIBLE);
            back.setOnClickListener(backClick());
            right.setOnClickListener(rightClick());
            actionBar.setCustomView(mActionBarView, lp);
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayShowTitleEnabled(false);
            if (Build.VERSION.SDK_INT >= 21) {
                getSupportActionBar().setElevation(0);
            }
        }
    }

    protected abstract int initLayout();


    @Override
    public void initView() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void loadData() {

    }

    public void finishRefreshOrLoadMore(RefreshLayout refreshLayout) {
        if (refreshLayout == null) {
            return;
        }
        if (refreshLayout.getState() == RefreshState.Refreshing) {
            refreshLayout.finishRefresh();
        }
        if (refreshLayout.getState() == RefreshState.Loading) {
            refreshLayout.finishLoadMore();
        }
    }

    private LoadingDialog dialog;

    public void showLoadingDialog() {
        if (dialog == null)
            dialog = new LoadingDialog(this);
        dialog.showDialog();
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
    }

    public void hideLoadingDialog(int code) {
        if (code == LoadingDialog.LOADING_SUCCESS) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (null != dialog && dialog.isShowing()) {
                        dialog.hideDialog();
                    }
                }
            }, 300);

        } else {
            if (null != dialog && dialog.isShowing()) {
                dialog.hideDialog();
            }
        }


    }


    public interface TitleListener {
        void onRightClick();

        void onBackClick();
    }

    private long lastClickTime = 0;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (isFastDoubleClick()) {
                return true;
            }
        }
        return super.dispatchTouchEvent(ev);
    }


    public boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        lastClickTime = time;
        return timeD <= 300;
    }

    @Override
    public Resources getResources() {
        //禁止app字体大小跟随系统字体大小调节
        Resources resources = super.getResources();
        if (resources != null && resources.getConfiguration().fontScale != 1.0f) {
            Configuration configuration = resources.getConfiguration();
            configuration.fontScale = 1.0f;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        return resources;
    }
}
