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.LayoutInflater;
import android.view.View;
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.MemberInfoData;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.data.source.TaskDataRepository;
import kr.co.hanwha.hifive.enums.AddTaskType;
import kr.co.hanwha.hifive.util.HFCommonUtil;

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

public class HFReceiverContainerView extends LinearLayout {
    private LinearLayout mLayoutContainer;

    private OnReceiverContainerListener mOnReceiverContainerListener;

    private ArrayList<MemberInfoData> mListOfMembers;

    public void setOnReceiverContainerListener(OnReceiverContainerListener onReceiverContainerListener) {
        this.mOnReceiverContainerListener = onReceiverContainerListener;
    }

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

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

    public HFReceiverContainerView(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_receiver_container, this, true);

        mLayoutContainer = (LinearLayout) findViewById(R.id.container_linearLayout);

        findViewById(R.id.receiver_linearLayout).setOnClickListener(onClickListener);
    }

    /**
     * 받는 사람 목록 표시
     *
     * @param listOfMembers
     */
    public void setMemberList(@NonNull ArrayList<MemberInfoData> listOfMembers) {
        mListOfMembers = listOfMembers;

        if (mListOfMembers == null || mListOfMembers.size() == 0) {
            findViewById(R.id.container_linearLayout).setVisibility(View.GONE);
            findViewById(R.id.hint_textView).setVisibility(View.VISIBLE);
            return;
        }

        findViewById(R.id.hint_textView).setVisibility(View.GONE);
        findViewById(R.id.container_linearLayout).setVisibility(View.VISIBLE);

        int screenWidth = HFCommonUtil.getScreenWidth(getContext());
        int margin = HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp100);
        int containerWidth = screenWidth - margin;
        mLayoutContainer.removeAllViews();

        boolean isAddedEtc = false;

        int count = mListOfMembers.size();
        for (int i = 0; i < mListOfMembers.size(); i++) {
            HFReceiverView view = new HFReceiverView(getContext());
            view.setMemberInfo(mListOfMembers.get(i));
            view.measure(0, 0);
            int memberWidth = view.getMeasuredWidth();

            HFReceiverEtcView etcView = new HFReceiverEtcView(getContext());
            etcView.setCount(count);
            etcView.measure(0, 0);
            int etcWidth = etcView.getMeasuredWidth();

            if (containerWidth - memberWidth > 0) {
                containerWidth -= memberWidth;
                count -= 1;
                mLayoutContainer.addView(view);

                if (containerWidth - etcWidth < 0 && count > 0 && isAddedEtc == false) {
                    etcView.setCount(count);
                    mLayoutContainer.addView(etcView);
                    isAddedEtc = true;
                    break;
                }
            } else if (isAddedEtc == false) {
                if (containerWidth - etcWidth > 0) {
                    containerWidth -= etcWidth;
                    etcView.setCount(count);
                    mLayoutContainer.addView(etcView);
                    isAddedEtc = true;
                    break;
                }
            }
        }
    }

    public void setAddTaskType(@NonNull AddTaskType addTaskType) {
        if (addTaskType != AddTaskType.NEW) {
            findViewById(R.id.selectMember_imageView).setVisibility(View.GONE);
        }
    }

    /**************************************************
     * OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.receiver_linearLayout:
                    if (mOnReceiverContainerListener != null)
                        mOnReceiverContainerListener.onAddMemberClicked();
                    break;
            }

        }
    };

    /**************************************************
     * OnReceiverContainerListener
     **************************************************/
    public interface OnReceiverContainerListener {
        void onAddMemberClicked();
    }
}