package com.tjzhxx.union.system;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.snackbar.Snackbar;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.tjzhxx.union.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 描述:
 *
 * @author yanghuaxiong
 * @create 2018/4/10 上午11:32
 */
public abstract class BaseActivity extends AppCompatActivity {
    @Nullable
    @BindView(R.id.toolbar)
    Toolbar toolbar;
//    @Nullable
//    @BindView(R.id.back)
//    LinearLayout back;

    @Nullable
    @BindView(R.id.app_name)
    public TextView appName;
    @Nullable
    @BindView(R.id.right_text)
    public TextView rightText;
    @Nullable
    @BindView(R.id.ll_right)
    public LinearLayout llRight;
    @Nullable
    @BindView(R.id.right_icon)
    public ImageView rightIcon;

    public String TAG = null;
    public Context mContext = null;

    //set layout
    public abstract int getContentViewLayoutID();

    //绑定控件，设置listener
    public abstract void bindViewAndEvent(Bundle savedInstanceState);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//锁定竖屏
        //防止截图
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);//不弹出软键盘
        super.onCreate(savedInstanceState);

//        getWindow().getDecorView().setFilterTouchesWhenObscured(false);
        this.TAG = this.getClass().getSimpleName();
        this.mContext = this;

        if (getContentViewLayoutID() != 0) {
            super.setContentView(getContentViewLayoutID());
            ButterKnife.bind(this);
            bindViewAndEvent(savedInstanceState);
            if (toolbar != null) {
                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
            }
//            if (back != null) {
//                back.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        finish();
//                    }
//                });
//            }
        }
//        StatusBarHelper.statusBarLightMode(this);
        MyApplication.getInstance().addActivity(this);
    }


    //初始化标题
    public void initTitle(String title) {
        if (appName != null) {
            appName.setText(title);
        }
    }

    //初始化标题和右边文字
    public void initTitle(String title, String rightTextS) {
        if (appName != null) {
            appName.setText(title);
        }
        if (rightText != null) {
            rightText.setVisibility(View.VISIBLE);
            rightText.setText(rightTextS);
        }
    }

    //初始化标题和右边图标
    public void initTitle(String title, int resId) {
        if (appName != null) {
            appName.setText(title);
        }
        if (llRight != null) {
            llRight.setVisibility(View.VISIBLE);
            rightIcon.setImageResource(resId);
        }
    }

    @Override
    protected void onDestroy() {
        MyApplication.getInstance().removeActivity(this);
        super.onDestroy();
    }

    public void showSnackBar(View topView, String msg) {
        if (null != msg && !TextUtils.isEmpty(msg)) {
            Snackbar.make(topView, msg, Snackbar.LENGTH_SHORT).show();
        }
    }

    public void showSnackBar(String msg) {
        if (null != msg && !TextUtils.isEmpty(msg)) {
            Snackbar.make(getWindow().getDecorView(), msg, Snackbar.LENGTH_SHORT).show();
        }
    }

    public void showProgressBar(String message) {

        HodingView.build().setContext(mContext).show(message);
    }

    public void hideProgressBar() {
        HodingView.build().hide();
    }
    //刷新完成以后刷新处理  防止以后改动
    public void refreshComplete(SmartRefreshLayout refreshLayout, Boolean isHasMore) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        } else if (refreshLayout.isLoading()) {
            if (isHasMore) {
                refreshLayout.finishLoadMore();
            }
        }
        if (isHasMore) {
            refreshLayout.setNoMoreData(false);
        } else {
            refreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    public void refreshError(SmartRefreshLayout refreshLayout) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        } else if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadMore();
        }
    }
}
