package com.tofly.ghrq_route.common.ui.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.user.domain.MessageUserBean;

import java.util.List;

public class UserListView {

    private int outside;
    private int show;
    private Context mContext;
    private List<MessageUserBean> userBeanList;

    /**
     * Context mContext:上下文； String title: 标题； String[] str:内部TextView显示的字符串的数组
     * int inid:每个TextView的id
     */

    public UserListView(Context context, List<MessageUserBean> users) {
        mContext = context;
        userBeanList = users;
    }

    public View getViewWithNoTitle() {
        LinearLayout rootLayout = new LinearLayout(mContext);
        rootLayout.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        rootLayout.setOrientation(LinearLayout.VERTICAL);

        int itemHeight = BaseApp.dip2px(26);

        int flag = userBeanList.size() % 3;
        if (flag != 0) {
            flag = 1;
        } else {
            flag = 0;
        }
        int outside = userBeanList.size() / 3 + flag;
        for (int i = 0; i < outside; i++) {
            final LinearLayout mLinearLayout = new LinearLayout(mContext);
            mLinearLayout.setLayoutParams(new LayoutParams(
                    LayoutParams.MATCH_PARENT, itemHeight));
            mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

            int show = i * 3;
            for (int j = 0; j < 3; j++) {
                final View rootView = LayoutInflater.from(mContext).inflate(R.layout.item_selected_user, mLinearLayout, false);
                TextView textName = (TextView) rootView.findViewById(R.id.text_user_name);
                ImageView imageView = (ImageView) rootView.findViewById(R.id.image_remove_user);
                if (show + j < userBeanList.size()){
                    final MessageUserBean userBean = userBeanList.get(show + j);
                    if (userBean != null && userBean.getRealName() != null){
                        textName.setText(userBean.getRealName());
                    }
                    imageView.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            mLinearLayout.removeView(rootView);
                            userBeanList.remove(userBean);
                        }
                    });
                }else {
                    rootView.setVisibility(View.INVISIBLE);
                }
                mLinearLayout.addView(rootView);
            }

            rootLayout.addView(mLinearLayout);
        }
        return rootLayout;
    }

    private boolean isLabelChecked(int id) {
        boolean checked = false;
        /*if (MiniApp.isLogined) {
            UserInfo userInfo = MiniApp.loginUserData;
            if (userInfo != null) {
                List<Lable> labels = userInfo.getLabels();
                for (int i = 0; i < labels.size(); i++) {
                    Lable lable = labels.get(i);
                    if (lable.getId() == id) {
                        checked = true;
                        break;
                    }
                }
            }
        }*/
        return checked;
    }
}
