package com.creyond.ecgdemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("Registered")
public class BaseActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

    }

    public void hideBack() {
        findViewById(R.id.iv_bar_back).setVisibility(View.GONE);
    }

    public void hideTitle() {
        findViewById(R.id.title_bar).setVisibility(View.GONE);
    }

    @Override
    public void setTitle(CharSequence title) {
//        super.setTitle(title);
        TextView tv_bar_title = findViewById(R.id.tv_bar_title);
        tv_bar_title.setText(title);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(R.layout.activity_base);
        if (layoutResID == R.layout.activity_base) {
            return;
        }
        LinearLayout layout = findViewById(R.id.content);
        LayoutInflater.from(this).inflate(layoutResID, layout);

        findViewById(R.id.iv_bar_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    public void error(String msg) {
        final TextView tv_toast = findViewById(R.id.tv_toast);
        tv_toast.setText(msg);
        tv_toast.setBackgroundColor(0xffff0000);
        tv_toast.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                tv_toast.setVisibility(View.GONE);
            }
        }, 2000);
    }

    public void success(String msg) {
        final TextView tv_toast = findViewById(R.id.tv_toast);
        tv_toast.setText(msg);
        tv_toast.setBackgroundColor(0xff4CB906);
        tv_toast.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                tv_toast.setVisibility(View.GONE);
            }
        }, 2000);
    }

}
