package com.android.chmo.ui.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.utils.CommonUtils;

/**
 * Created by Administrator on 2018/7/30.
 */

public class TopBar extends RelativeLayout {
    private ImageView backBtn;
    private TextView titleView;
    private TextView rightView;

    private String title = "";
    private boolean showBack = true;


    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        if (typedArray != null) {
            title = typedArray.getString(R.styleable.TopBar_topTitle);
            showBack = typedArray.getBoolean(R.styleable.TopBar_showBack, true);
            typedArray.recycle();
        }
        init(context);
    }


    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.top_bar, this);
        backBtn = (ImageView) findViewById(R.id.back);
        titleView = (TextView) findViewById(R.id.title);
        rightView = (TextView) findViewById(R.id.right);

        backBtn.setVisibility(showBack ? View.VISIBLE : View.GONE);
        titleView.setText(title);
        setPadding(0, CommonUtils.getStatusBarHeight(context), 0, 1);
        setBackgroundColor(Color.WHITE);

        backBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity activity = (Activity)getContext();
                activity.finish();
            }
        });
    }


    public void setTitle(String title) {
        this.title = title;
        titleView.setText(title);
    }

    public void setRightBtn(String text, OnClickListener listener) {
        rightView.setText(text);
        rightView.setVisibility(View.VISIBLE);
        rightView.setOnClickListener(listener);
    }

}
