package com.kdanmobile.kdan_others_library_for_android.card;

import android.content.Context;
import android.graphics.drawable.Drawable;
import androidx.annotation.AttrRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.kdanmobile.kdan_others_library_for_android.R;

/**
 * Created by Wei on 2017/5/29.
 */

public class BaseCard extends FrameLayout {

    private View rootView;
    private ImageView mediaImageView;
    private TextView primaryTextTextView;
    private TextView supportingTextTextView;
    private Button action1Button, action2Button, action3Button;

    public BaseCard(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public BaseCard(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public BaseCard(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public ImageView getMediaImageView() {
        return mediaImageView;
    }

    public TextView getPrimaryTextTextView() {
        return primaryTextTextView;
    }

    public TextView getSupportingTextTextView() {
        return supportingTextTextView;
    }

    public Button getAction1Button() {
        return action1Button;
    }

    public Button getAction2Button() {
        return action2Button;
    }

    public Button getAction3Button() {
        return action3Button;
    }

    public void setMedia(@DrawableRes int id) {
        mediaImageView.setImageResource(id);
    }

    public void setMedia(Drawable d) {
        mediaImageView.setImageDrawable(d);
    }

    public void setPrimaryText(@StringRes int id) {
        setTextAndChangeVisibility(primaryTextTextView, id);
    }

    public void setPrimaryText(String s) {
        setTextAndChangeVisibility(primaryTextTextView, s);
    }

    public void setSupportingText(@StringRes int id) {
        setTextAndChangeVisibility(supportingTextTextView, id);
    }

    public void setSupportingText(String s) {
        setTextAndChangeVisibility(supportingTextTextView, s);
    }

    public void setAction1Text(String s) {
        setTextAndChangeVisibility(action1Button, s);
    }

    public void setAction2(String text, View.OnClickListener onClickListener) {
        setTextAndOnClickListener(action2Button, text, onClickListener);
    }

    public void setAction3(String text, View.OnClickListener onClickListener) {
        setTextAndOnClickListener(action3Button, text, onClickListener);
    }

    private void setTextAndOnClickListener(TextView textView, @StringRes int id, View.OnClickListener l) {
        setTextAndChangeVisibility(textView, id);
        textView.setOnClickListener(l);
    }

    private void setTextAndOnClickListener(TextView textView, String text, View.OnClickListener l) {
        setTextAndChangeVisibility(textView, text);
        textView.setOnClickListener(l);
    }

    private void setTextAndChangeVisibility(TextView textView, @StringRes int id) {
        textView.setVisibility(id == -1 ? GONE : VISIBLE);
        textView.setText(id);
    }

    private void setTextAndChangeVisibility(TextView textView, String text) {
        textView.setVisibility(TextUtils.isEmpty(text) ? GONE : VISIBLE);
        textView.setText(text);
    }

    private void initView(@NonNull Context context) {
        rootView = inflate(context, R.layout.widget_base_card, this);
        primaryTextTextView = (TextView) rootView.findViewById(R.id.textView_baseCard_primaryText);
        mediaImageView = (ImageView) rootView.findViewById(R.id.imageView_baseCard_media);
        primaryTextTextView = (TextView) rootView.findViewById(R.id.textView_baseCard_primaryText);
        supportingTextTextView = (TextView) rootView.findViewById(R.id.textView_baseCard_supportingText);
        action1Button = (Button) rootView.findViewById(R.id.button_baseCard_action1);
        action2Button = (Button) rootView.findViewById(R.id.button_baseCard_action2);
        action3Button = (Button) rootView.findViewById(R.id.button_baseCard_action3);
    }
}
