package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.data.model.CampaignData;
import kr.co.hanwha.hifive.data.model.MemberInfoData;

public class HFCampaignContainerView extends LinearLayout {
    private TextView mTextViewCampaign;
    private TextView mTextViewCampaignHint;
    private ImageButton mImageButtonClearCampaign;

    private OnCampaignContainerListener mOnCampaignContainerListener;

    private ArrayList<MemberInfoData> mListOfMembers;

    public void setOnCampaignContainerListener(OnCampaignContainerListener onCampaignContainerListener) {
        this.mOnCampaignContainerListener = onCampaignContainerListener;
    }

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

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

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

    private void initialize(Context context, @Nullable AttributeSet attrs) {
        if (isInEditMode()) return;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_hf_campaign_container, this, true);

        mTextViewCampaign = (TextView) findViewById(R.id.campaign_textView);
        mTextViewCampaignHint = (TextView) findViewById(R.id.campaignHint_textView);
        mImageButtonClearCampaign = (ImageButton) findViewById(R.id.clearCampaign_imageButton);

        findViewById(R.id.campaign_linearLayout).setOnClickListener(onClickListener);
        mImageButtonClearCampaign.setOnClickListener(onClickListener);
    }

    public void setCampaignInfo(@Nullable CampaignData data) {
        if (data != null && TextUtils.isEmpty(data.getCampaign_desc()) == false) {
            mTextViewCampaign.setText(data.getCampaign_desc());
            mTextViewCampaign.setVisibility(View.VISIBLE);
            mTextViewCampaignHint.setVisibility(View.GONE);
            mImageButtonClearCampaign.setVisibility(View.VISIBLE);
            mImageButtonClearCampaign.setOnClickListener(onClickListener);
        } else {
            mTextViewCampaign.setText("");
            mTextViewCampaign.setVisibility(View.GONE);
            mTextViewCampaignHint.setVisibility(View.VISIBLE);
            mImageButtonClearCampaign.setVisibility(View.INVISIBLE);
            mImageButtonClearCampaign.setOnClickListener(null);
        }
    }

    /**************************************************
     * OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.campaign_linearLayout:
                    if (mOnCampaignContainerListener != null)
                        mOnCampaignContainerListener.onSelectCampaign();
                    break;
                case R.id.clearCampaign_imageButton:
                    if (mOnCampaignContainerListener != null)
                        mOnCampaignContainerListener.onClearCampaign();
                    break;
            }

        }
    };

    /**************************************************
     * OnReceiverContainerListener
     **************************************************/
    public interface OnCampaignContainerListener {
        void onSelectCampaign();

        void onClearCampaign();
    }
}