package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;

public class HFMyHomeHeaderView extends RelativeLayout {
    private TextView mTextViewName;
    private TextView mTextViewEmail;
    private ImageButton mImageButtonPlus;

    private OnMyHomeClickListener onMyHomeClickListener;

    public void setOnMyHomeClickListener(OnMyHomeClickListener onMyHomeClickListener) {
        this.onMyHomeClickListener = onMyHomeClickListener;
    }

    public HFMyHomeHeaderView(Context context) {
        super(context);
        initialize(context, null);
    }

    public HFMyHomeHeaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public HFMyHomeHeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs);
    }

    private void initialize(Context context, @Nullable AttributeSet attrs) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_hf_my_home_header, this, true);

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyHomeHeaderView);
        initLayout(typedArray);

        initCallback();
    }

    private void initLayout(TypedArray typedArray) {
        mTextViewName = (TextView) findViewById(R.id.name_textView);
        mTextViewEmail = (TextView) findViewById(R.id.email_textView);
        mImageButtonPlus = (ImageButton) findViewById(R.id.plus_imageButton);
        boolean isPlusButtonVisible = typedArray.getBoolean(R.styleable.MyHomeHeaderView_plusButtonVisible, true);
        mImageButtonPlus.setVisibility(isPlusButtonVisible ? View.VISIBLE : View.GONE);
    }

    private void initCallback() {
        mImageButtonPlus.setOnClickListener(onClickListener);
    }

    public void setMemberInfo(@NonNull String memberName, @NonNull String loginId) {
        mTextViewName.setText(memberName);
        mTextViewEmail.setText(loginId);
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.plus_imageButton:
                    onMyHomeClickListener.onPlusClick();
                    break;
            }
        }
    };


    /**************************************************
     * OnSettingProfileListener - interface
     **************************************************/
    public interface OnMyHomeClickListener {
        void onPlusClick();
    }
}