package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.util.HFCommonUtil;

/**
 * Created by sanghoonkwon on 2018. 1. 12..
 */

public class HFTagContainerView extends LinearLayout {
    private LinearLayout mLayoutTagContainer;

    private OnSelectedTagListener mOnSelectedTagListener;

    private ArrayList<String> mListOfTags;

    public void setOnSelectedTagListener(OnSelectedTagListener onSelectedTagListener) {
        this.mOnSelectedTagListener = onSelectedTagListener;
    }

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

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

    public HFTagContainerView(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_tag_container, this, true);

        mLayoutTagContainer = (LinearLayout) findViewById(R.id.tagContainer_linearLayout);
    }

    public void setTagList(@NonNull ArrayList<String> listOfTags) {
        mListOfTags = listOfTags;

        if (mListOfTags == null || mListOfTags.size() == 0) {
            return;
        }
        findViewById(R.id.tagContainer_linearLayout).setVisibility(View.VISIBLE);

        int screenWidth = HFCommonUtil.getScreenWidth(getContext());
        int margin = 0; // Check margin

        int lastLine = 0;
        int containerWidth = screenWidth - margin;
        mLayoutTagContainer.removeAllViews();
        mLayoutTagContainer.addView(getHorizontalLayout(Integer.toString(lastLine)));

        for (int i = 0; i < listOfTags.size(); i++) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.view_tag_round, mLayoutTagContainer, false);
            TextView textViewTitleTag = view.findViewById(R.id.title_textView);

            view.setTag(listOfTags.get(i));
            textViewTitleTag.setText(listOfTags.get(i));

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String tag = (String) view.getTag();

                    if (mOnSelectedTagListener != null)
                        mOnSelectedTagListener.onTagClicked(tag);
                }
            });

            view.measure(0, 0);
            int measuredWidth = view.getMeasuredWidth();
            if (containerWidth - measuredWidth < 0) {
                lastLine += 1;
                containerWidth = screenWidth - margin;
                mLayoutTagContainer.addView(getHorizontalLayout(Integer.toString(lastLine)));
            }
            containerWidth -= measuredWidth;

            ((LinearLayout) mLayoutTagContainer.findViewWithTag(Integer.toString(lastLine))).addView(view);
        }
    }

    private LinearLayout getHorizontalLayout(String tag) {
        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setGravity(Gravity.CENTER_VERTICAL);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.setTag(tag);
        return linearLayout;
    }

    public ArrayList<String> getListOfTags() {
        return mListOfTags;
    }

    /**************************************************
     * OnSelectedTagListener
     **************************************************/
    public interface OnSelectedTagListener {
        void onTagClicked(@NonNull String tag);
    }
}
