package com.ez.ertc.meeting.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ez.ertc.meeting.R;
import com.ez.ertc.meeting.R2;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MtTitleBar extends FrameLayout {

    @BindView(R2.id.rlTitleBarRoot)
    RelativeLayout rlTitleBarRoot;
    @BindView(R2.id.flNavBack)
    FrameLayout flNavBack;
    @BindView(R2.id.ivNavBack)
    MtTextView ivNavBack;
    @BindView(R2.id.tvNavTittle)
    TextView tvNavTittle;
    @BindView(R2.id.ivNavRight)
    MtTextView ivNavRight;
    @BindView(R2.id.ivNavRightSub)
    MtTextView ivNavRightSub;

    private ColorStateList rightTextColor,leftTextColor;
    private int tbBackground,tbTitleColor;
    private String resLeft,resTitle,resRight,title;
    private float tbLeftSize,tbRightSize;

    public MtTitleBar(Context context) {
        this(context, null);
    }

    public MtTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MtTitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ezmTitleBar, defStyle, R.style.ezmTitleBar_default);
        resTitle = a.getString(R.styleable.ezmTitleBar_ezm_tbTitle);
        resLeft = a.getString(R.styleable.ezmTitleBar_ezm_tbLeft);
        resRight = a.getString(R.styleable.ezmTitleBar_ezm_tbRight);
        rightTextColor = a.getColorStateList(R.styleable.ezmTitleBar_ezm_tbRightTextColor);
        leftTextColor = a.getColorStateList(R.styleable.ezmTitleBar_ezm_tbLeftTextColor);
        tbBackground = a.getColor(R.styleable.ezmTitleBar_ezm_tbBackground,0);
        tbTitleColor = a.getColor(R.styleable.ezmTitleBar_ezm_tbTitleColor,0);
        tbLeftSize = a.getDimension(R.styleable.ezmTitleBar_ezm_tbLeftSize,0);
        tbRightSize = a.getDimension(R.styleable.ezmTitleBar_ezm_tbRightSize,0);
        a.recycle();
        init();
    }

    private void init() {
        View root = LayoutInflater.from(getContext()).inflate(R.layout.ys_mt_title_bar, this);
        ButterKnife.bind(this,root);
        if (!TextUtils.isEmpty(resTitle)){
            tvNavTittle.setText(resTitle);
        }
        if (!TextUtils.isEmpty(resLeft)){
            ivNavBack.setText(resLeft);
        }
        if (!TextUtils.isEmpty(resRight)){
            ivNavRight.setText(resRight);
        }
        if (tbLeftSize != 0){
            ivNavBack.setTextSize(TypedValue.COMPLEX_UNIT_PX,tbLeftSize);
        }
        if (tbRightSize != 0){
            ivNavRight.setTextSize(TypedValue.COMPLEX_UNIT_PX,tbRightSize);
        }
        ivNavBack.setTextColor(leftTextColor);
        ivNavRight.setTextColor(rightTextColor);
        if (tbBackground != 0){
            rlTitleBarRoot.setBackgroundColor(tbBackground);
        }
        if (tbTitleColor != 0){
            tvNavTittle.setTextColor(tbTitleColor);
        }
        flNavBack.setOnClickListener(new OnClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {
                if (getContext() instanceof Activity){
                    ((Activity)getContext()).onBackPressed();
                }
            }
        });
    }


    public void setTitle(int resId){
        tvNavTittle.setText(resId);
        this.title = getResources().getString(resId);
    }
    public String getTitle(){
        return title;
    }
    public void setTitle(CharSequence title){
        tvNavTittle.setText(title);
        this.title = title == null ? null : title.toString();
    }

    public void setNavBack(int resId){
        ivNavBack.setText(resId);
    }
    public void setNavBackSize(int size){
        ivNavBack.setTextSize(TypedValue.COMPLEX_UNIT_PX,size);
    }

    public void setNavRight(int resId){
        ivNavRight.setText(resId);
    }
    public void setNavRight(String txt){
        ivNavRight.setText(txt);
    }
    public void setNavRightTextColor(int resId){
        ivNavRight.setTextColor(resId);
    }
    public void setNavRightTextColor(ColorStateList resId){
        ivNavRight.setTextColor(resId);
    }
    public void setRightClickListener(OnClickListener onClickListener){
        ivNavRight.setOnClickListener(onClickListener);
    }

    public void setNavBackListener(OnClickListener navBackListener){
        flNavBack.setOnClickListener(navBackListener);
    }

    public void setRightSubClickListener(OnClickListener onClickListener){
        ivNavRightSub.setOnClickListener(onClickListener);
    }

    public TextView getTvNavTittle(){
        return tvNavTittle;
    }
    public MtTextView getIvNavRight() {
        return ivNavRight;
    }

    public MtTextView getIvNavRightSub() {
        return ivNavRightSub;
    }

    public FrameLayout getNavBack() {
        return flNavBack;
    }
}