package com.qhclh.ytzh.work.carsmannage;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.utils.ActivityCollector;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;


/**
 * Created by 青花瓷 on 2017/7/19.
 */

public abstract class BaseActivity1 extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setLayoutId());
        ButterKnife.bind(this);
        ActivityCollector.addActivity(this);
        initView();
        initData();
        initOper();
    }

    protected abstract int setLayoutId();
    protected abstract void initView();
    protected abstract void initData();
    protected abstract void initOper();
    ///< toolbar设置
    public void initToolbar(Toolbar toolbar, String title, View.OnClickListener listener) {
        if (toolbar == null) {
            return;
        }
        TextView tvTitle = findById(toolbar, R.id.tv_toolbar_title1);
        if (tvTitle != null) {
            tvTitle.setText(title == null ? "" : title);
        }
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);
        }
        if (listener != null) {
            toolbar.setNavigationIcon(R.drawable.fanhui1_3x);
            toolbar.setNavigationOnClickListener(listener);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        try {
            MobclickAgent.onResume(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        try {
            MobclickAgent.onPause(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


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

    public void finishAll(){ActivityCollector.finishAll();}

    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    protected <T extends View> T findById(@NonNull View view, @IdRes int resId) {
        return ButterKnife.findById(view, resId);
    }
}
