package com.gingersoft.gsa.cloud.table.mvp.ui.adapter.meal;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gingersoft.gsa.cloud.base.common.bean.mealManage.MyOrderManage;
import com.gingersoft.gsa.cloud.base.utils.MoneyUtil;
import com.gingersoft.gsa.cloud.database.bean.Food;
import com.gingersoft.gsa.cloud.table.R;
import com.gingersoft.gsa.cloud.table.R2;
import com.gingersoft.gsa.cloud.base.common.bean.OrderDetail;
import com.gingersoft.gsa.cloud.table.mvp.model.constant.MealConstant;
import com.gingersoft.gsa.cloud.table.mvp.presenter.MealStandPresenter;
import com.gingersoft.gsa.cloud.table.mvp.presenter.OrderContentPresenter;
import com.gingersoft.gsa.cloud.table.mvp.ui.activity.MealStandActivity;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;

import java.util.List;

import butterknife.BindView;


/**
 * 作者：ELEGANT_BIN
 * 版本：1.6.0
 * 创建日期：2018/9/16
 * 修订历史：2018/9/17
 * 描述：
 */

public class SelectMealAdapter extends DefaultAdapter<OrderDetail> {

    private Context mContext;

    private MealStandActivity mMealStandActivity;
    private MealStandPresenter mMealStandPresenter;
    private OrderContentPresenter mOrderContentPresenter;

    private int BillListFontSize = 18;
    private String carshStr = "$";

    private int mOrderNumberShowSize;
    private int mOrderNumberFontSize;
    private int mFoodFontSize;

    private int mColwidth;
    private int mColHeight;
    private int mLayoutQtyHeight;

    private int mainStartPos = 0;
    private int mainEndPos = 0;

    private int select_position = -1;
    private int select_start_position = 0;
    private int select_end_position = 0;
    public int select_full_start_position = 0;
    public int select_full_end_position = 0;

    private boolean isRemove = false;

    public SelectMealAdapter(Context context, List<OrderDetail> data, BasePresenter presenter) {
        super(data);
        this.mContext = context;
        if (presenter instanceof MealStandPresenter) {
            this.mMealStandPresenter = (MealStandPresenter) presenter;
            this.mMealStandActivity = (MealStandActivity) context;
        } else if (presenter instanceof OrderContentPresenter) {
            this.mOrderContentPresenter = (OrderContentPresenter) presenter;
        }
//        this.mOrderNumberShowSize = GSAApplication.androidSetting.getOrderNumberChildShowSize();
//        this.mOrderNumberFontSize = GSAApplication.androidSetting.getOrderNumberChildFontSize();
//        this.mFoodFontSize = GSAApplication.androidSetting.getFoodFontSize();
//        this.mColwidth = (int) ((DeviceUtils.getScreenWidth(context) / GSAApplication.androidSetting.getFoodCol()) - 4);
//        this.mColHeight = GSAApplication.androidSetting.getFoodBtnHeight();
//        this.mLayoutQtyHeight = GSAApplication.androidSetting.getLayoutQtyHeight();
//
//        this.BillListFontSize = GSAApplication.androidSetting.getFoodlListFontSize();
//        this.carshStr = LanguageUtils.get_language_system(context, "order.cashstr", "$");
    }

    @Override
    public int getItemCount() {
        return mInfos == null ? 0 : mInfos.size();
    }

    @Override
    public SelectMealItemHolder getHolder(View v, int viewType) {
        return new SelectMealItemHolder(v);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.meal_item_select_meal_stand;
    }

    class SelectMealItemHolder extends BaseHolder<OrderDetail> implements View.OnClickListener {

        @BindView(R2.id.rl_container)
        RelativeLayout rl_container;

        @BindView(R2.id.rl_select)
        RelativeLayout rl_select;
        @BindView(R2.id.ll_tvname)
        RelativeLayout ll_tvname;

        @BindView(R2.id.tv_number)
        TextView tv_number;
        @BindView(R2.id.tv_food_table_mode)
        TextView tv_food_table_mode;
        @BindView(R2.id.tv_modify_before_number)
        TextView tv_modify_before_number;
        @BindView(R2.id.tv_name)
        TextView tv_name;
        @BindView(R2.id.tv_money)
        TextView tv_money;

        @BindView(R2.id.tv_D)
        TextView tv_D;
        @BindView(R2.id.tv_S)
        TextView tv_S;
        @BindView(R2.id.tv_D_1)
        TextView tv_D_1;
        @BindView(R2.id.tv_S_1)
        TextView tv_S_1;

        @BindView(R2.id.img_lock)
        ImageView iv_lock;
        @BindView(R2.id.tv_discount_money)
        TextView tv_discount_money;
        @BindView(R2.id.tv_mix)
        TextView tv_mix;

        @BindView(R2.id.iv_add_count)
        ImageView iv_add_count;
        @BindView(R2.id.iv_delete_count)
        ImageView iv_delete_count;

        public SelectMealItemHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onClick(View view) {
            if (mOnItemClickListener != null) {
                int position = this.getPosition();
                if (position < mInfos.size()) {
                    setSelectPosition(position);
                    //设置当前选中的食品或食品组
                    mOnItemClickListener.onItemClick(mInfos.get(position), position);
                }
            }
        }

        @Override
        public void setData(OrderDetail datasBean, int position) {

            setViewVisibility(datasBean);
            if (datasBean.isVisible()) {
                rl_container.setVisibility(View.VISIBLE);
                setName(datasBean);
                setNumber(datasBean);
                setMoney(datasBean);
                setMealCountLayout(datasBean, position);
                setColor(datasBean);
                setTextSize();
            } else {
                rl_container.setVisibility(View.INVISIBLE);
            }
        }

        private void setViewVisibility(OrderDetail datasBean) {

            tv_D.setVisibility(View.INVISIBLE);
            tv_D_1.setVisibility(View.INVISIBLE);
            tv_discount_money.setVisibility(View.INVISIBLE);

//            if (datasBean.isModify()) {
//                tv_modify_before_number.setText(String.valueOf(datasBean.getModifyBeforeNumber()));
//                tv_modify_before_number.setVisibility(View.VISIBLE);
//            } else {
//                tv_modify_before_number.setVisibility(View.INVISIBLE);
//            }
//
//            if (datasBean.getMix_DETAIL_ID() > 0) {
//                tv_mix.setText(String.valueOf(datasBean.getMix_DETAIL_ID()));
//                tv_mix.setVisibility(View.VISIBLE);
//            } else {
//                tv_mix.setVisibility(View.INVISIBLE);
//            }
        }

        private void setMealCountLayout(OrderDetail datasBean, int position) {
//            if (datasBean.isnew &&
//                    mOrderContentPresenter != null &&
//                    select_position == position &&
//                    datasBean.getDiscount_id() == 0) {
                if (datasBean.isNew() &&
                        mOrderContentPresenter != null &&
                        select_position == position ) {

                iv_add_count.setVisibility(View.VISIBLE);
                iv_delete_count.setVisibility(View.VISIBLE);
                iv_add_count.setOnClickListener(v -> {
                    if (mOnItemClickListener != null)
                        mOnItemClickListener.onMealCountAdd(datasBean, position);
                });
                iv_delete_count.setOnClickListener(v -> {
                    if (mOnItemClickListener != null)
                        mOnItemClickListener.onMealCountDelete(datasBean, position);
                });
            } else {
                iv_add_count.setVisibility(View.INVISIBLE);
                iv_delete_count.setVisibility(View.INVISIBLE);
            }
        }

        private void setName(OrderDetail datasBean) {
            int flevel = datasBean.getItemType();
            String name = datasBean.getProductName();
            switch (flevel) {
                case 1:
                    tv_number.setGravity(Gravity.LEFT);
                    break;
                case 2:
                    name = "  " + name;
                    tv_number.setGravity(Gravity.CENTER);
                    break;
                case 3:
                    name = "      " + name;
                    tv_number.setGravity(Gravity.RIGHT);
                    break;
            }
            tv_name.setText(name);

            int selected = datasBean.getSelected();
            if (selected != 0) {
                //作为主体被选中 开启跑马灯效果
                tv_name.setFocusable(true);
                tv_name.setEllipsize(TextUtils.TruncateAt.MARQUEE);//设置旋转
                tv_name.setMarqueeRepeatLimit(-1);//-1为死循环，1为1次
                tv_name.setSelected(true);//设置不获取焦点就可以转
                tv_name.setFocusableInTouchMode(true);
            } else {
                //关闭跑马灯效果
                tv_name.setFocusable(false);
                tv_name.setMarqueeRepeatLimit(0);
                tv_name.setSelected(false);
                tv_name.setFocusableInTouchMode(false);
            }
        }

        private void setNumber(OrderDetail datasBean) {
            int number = datasBean.getNumber();
            if (number == 0) {
                //细项、折扣不需要显示数量
                tv_number.setText("");
            } else {
                tv_number.setText(String.valueOf(number));
            }
        }

        private void setMoney(OrderDetail datasBean) {
            double price = datasBean.getPrice();
            if (price != 0) {
                tv_money.setText(carshStr + MoneyUtil.get_ItemDecimals_money(price));
            } else {
                tv_money.setText("");
            }
        }

        private void setColor(OrderDetail datasBean) {
            int selected = datasBean.getSelected();
            switch (selected) {
                case 0:
                    rl_container.setBackgroundColor(ArmsUtils.getColor(mContext, R.color.theme_white_color));
                    rl_select.setBackgroundColor(ArmsUtils.getColor(mContext, R.color.theme_white_color));
                    ll_tvname.setBackgroundColor(ArmsUtils.getColor(mContext, R.color.theme_white_color));
                    break;
                case 1:
                    rl_container.setBackgroundColor(ArmsUtils.getColor(mContext, R.color.itemSelected));
                    rl_select.setBackgroundColor(ArmsUtils.getColor(mContext, R.color.itemSelected));// Color.YELLOW);
                    ll_tvname.setBackgroundResource(R.color.itemSelected);
                    break;
                case 2:
                case 3:
                    rl_container.setBackgroundColor(ArmsUtils.getColor(mContext, R.color.itemSelected));
                    rl_select.setBackgroundColor(ArmsUtils.getColor(mContext, R.color.itemSelected));// Color.YELLOW);
                    ll_tvname.setBackgroundResource(R.color.mainSelected);
                    break;
            }
//            }
            //设置Item选中颜色
            if (datasBean.isNew() == false) {
                iv_lock.setVisibility(View.INVISIBLE);
                int number = datasBean.getNumber();
//                if (number == 0) {
//                    tv_name.setTextColor(ArmsUtils.getColor(mContext,R.color.theme_red_color));
//                    tv_number.setTextColor(ArmsUtils.getColor(mContext,R.color.theme_red_color));
//                    tv_money.setTextColor(ArmsUtils.getColor(mContext,R.color.theme_red_color));
//                } else {
                    tv_name.setTextColor(ArmsUtils.getColor(mContext, R.color.Grass_green));
                    tv_number.setTextColor(ArmsUtils.getColor(mContext, R.color.Grass_green));
                    tv_money.setTextColor(ArmsUtils.getColor(mContext, R.color.Grass_green));
//                }
            } else {
                iv_lock.setVisibility(View.INVISIBLE);
                tv_number.setTextColor(ArmsUtils.getColor(mContext, R.color.black));
                tv_name.setTextColor(ArmsUtils.getColor(mContext, R.color.black));
                tv_money.setTextColor(ArmsUtils.getColor(mContext, R.color.black));
            }
        }

        public void setTextSize() {
            tv_money.setTextSize(BillListFontSize);
            tv_name.setTextSize(BillListFontSize);
            tv_number.setTextSize(BillListFontSize);
        }
    }

    public int addFoodItem(OrderDetail datasBean) {
        int addPosition = addPositionItem(datasBean);
        setSelectPosition(select_position);
        setIndex();
        setItemNo(datasBean);
        setMyOrderManageSelectPosition();
        return addPosition;
    }

    private int addPositionItem(OrderDetail addFoodBean) {

        int addPosition = select_position;

        switch (addFoodBean.getItemType()) {
            case 1:
                addPosition = select_position = mInfos.size();
                break;
            case 2:
                addPosition = select_full_end_position + 1;
                break;
            case 3:
                if (mInfos.get(select_position).getItemType() == 3) {
                    addPosition = select_end_position + 1;
                    select_position = addPosition;
                } else {
                    switch (mInfos.get(select_position).getItemType()) {
                        case 1:
                            if (hasChild3Food(mInfos.get(select_position))) {
                                addPosition = select_end_position + 1;
                            } else {
                                addPosition = select_position + 1;
                            }
                            break;
                        case 2:
                            if (hasChildFood(mInfos.get(select_position))) {
                                addPosition = select_end_position + 1;
                            } else {
                                addPosition = select_position + 1;
                            }
                            break;
                        case 3:
                            int edgeIndex2 = getChild2EdgeIndex();
                            addPosition = edgeIndex2 + 1;
                            select_position = addPosition;
                            break;
                    }
                }
                break;
        }
        if (addPosition >= mInfos.size()) {
            mInfos.add(addFoodBean);
        } else {
            mInfos.add(addPosition, addFoodBean);
        }
        return addPosition;
    }

    /**
     * 長按刪除食品
     */
    public void removeAllNewFoodItem() {
        for (int i = mInfos.size() - 1; i >= 0; i--) {
            OrderDetail orderItem = mInfos.get(i);
            if (orderItem != null && orderItem.isNew()) {
                mInfos.remove(i);
                notifyItemChanged(i);
                Food orderBean = mMealStandPresenter.getOrderFoodById(orderItem.getId());
                if (orderBean != null) {
                    mMealStandPresenter.setUserMaxQty(orderBean);
                }
            }
        }

        if (mInfos.size() == 0)
            //全部刪除
            return;

        setIndex();

        setSelectPosition(select_position);

        setMyOrderManageSelectPosition();

        mMealStandActivity.showViewModeVisibility(MealConstant.food_group_ViewMode, MealConstant.food_ViewMode);
    }

    /**
     * 删除食品
     */
    public int removeFoodItem() {

        removeIntactFoodItem();

        if (mInfos.size() == 0) {
            //全部删除重置选中下标
            select_position = -1;
        } else {
            if (select_position == mInfos.size()) {
                select_position -= 1;
            }
        }

        setIndex();

        setSelectPosition(select_position);

        setMyOrderManageSelectPosition();

        if (mMealStandActivity != null)
            mMealStandActivity.showViewModeVisibility(MealConstant.food_group_ViewMode, MealConstant.food_ViewMode);

        return select_position;
    }

    /**
     * 删除食品子项及折扣
     */
    public void removeChildItem() {
        OrderDetail orderDetail=   mInfos.get(select_position);
        if (select_position > -1) {
            switch (orderDetail.getItemType()) {
                case 1:
                    //第一层下方有细项 从下方往上删
//                    if (select_end_position != 0 && select_end_position > select_position) {
                    if (select_full_end_position != 0 && select_full_end_position > select_position) {
                        for (int i = select_full_end_position; i > select_position; i--) {
                            OrderDetail orderBean = mInfos.get(i);
                            boolean isParentChild = orderBean.getParentIndex() == orderDetail.getMyIndex();
                            if (orderBean.getItemType() == 3 && !isParentChild) {
                                //第二层 下面有第三层子项也要一并删除
                                removeAllChild3ByCombo(orderBean);
                            } else {
                                mInfos.remove(i);
                                notifyItemChanged(i);
                            }
                            break;
                        }
                    } else {
                        //第一层没有子食品 直接退出
                        return;
                    }
                    break;
                case 2:
                    if (select_end_position != 0 &&
                            select_end_position > select_position &&
                            hasChild3Food(orderDetail)) {
                        //第二层 下面有第三层子项也要一并删除
                        for (int i = select_end_position; i >= select_position; i--) {
                            mInfos.remove(i);
                            notifyItemChanged(i);
                        }
                    } else {
//                        if (mInfos.get(select_position).comid.equals("discount")) {
//                            //如果刪除的是折扣，這裡初始化父食品D標識
//                            int parent_index = mInfos.get(select_position).getParent_index() -1;
//                            if (parent_index > -1 && parent_index < mInfos.size()) {
//                                mInfos.get(parent_index).setDiscount_double1(true);
//                            }
//                        }
                        mInfos.remove(select_position);
                        notifyItemChanged(select_position);
                    }
                    break;
                case 3:
                    mInfos.remove(select_position);
                    notifyItemChanged(select_position);
                    break;
            }
        }

        if (select_position >= mInfos.size()) {
            select_position = mInfos.size() - 1;
        }

        if (mInfos.size() == 0) {
            //全部删除重置选中下标
            select_position = -1;
        } else {
            switch (orderDetail.getItemType()) {
                case 1:
                    break;
                case 2:
                    int comboIndex = getComboEdgeIndex();
                    if (comboIndex != (orderDetail.getMyIndex() - 1)) {
                        select_position -= 1;
                    } else {
                        if (select_position == mInfos.size()) {
                            select_position -= 1;
                        }
                    }
                    break;
                case 3:
                    int edgeIndex = getChild2EdgeIndex();
                    if (edgeIndex != (orderDetail.getMyIndex() - 1)) {
                        select_position -= 1;
                    } else {
                        if (select_position == mInfos.size()) {
                            select_position -= 1;
                        }
                    }
                    break;
            }
        }
        setIndex();
        setSelectPosition(select_position);
        setMyOrderManageSelectPosition();
    }

    private void removeIntactFoodItem() {
        int start_position = select_full_start_position + 1;
        int end_position = select_full_end_position + 1;
        for (int i = mInfos.size() - 1; i >= 0; i--) {
            int my_index = mInfos.get(i).getMyIndex();
            if (start_position <= my_index && my_index <= end_position) {
                if (mInfos.get(i).isNew()) {
                    mInfos.remove(i);
                    notifyItemChanged(i);
                }
            }
        }
    }

    /**
     * 删除第二层子食品下所有第三层备注
     *
     * @param orderDetail
     * @return
     */
    private boolean removeAllChild3ByCombo(OrderDetail orderDetail) {
        boolean remove = false;
        for (int i = mInfos.size() - 1; i >= 0; i--) {
            boolean accordChild3 = mInfos.get(i).getParentIndex() == orderDetail.getParentIndex() || mInfos.get(i).getMyIndex() == orderDetail.getParentIndex();
            if (mInfos.get(i).getItemType() != 1 && accordChild3) {
                mInfos.remove(i);
                notifyItemChanged(i);
                remove = true;
            }
        }
        return remove;
    }
    
    public void setMyOrderManageSelectPosition() {
        MyOrderManage.getInstance().setSelstartpostion(select_start_position);
        MyOrderManage.getInstance().setSelendpostion(select_end_position);
        MyOrderManage.getInstance().setSelmainendpostion(mainEndPos);
        MyOrderManage.getInstance().setSelmainstartpositon(mainStartPos);
        MyOrderManage.getInstance().setSelpostion(select_position);
    }

    /**
     * 获取食品第细项最后一个子项位置
     *
     * @return
     */
    private int getChild2EdgeIndex() {
        int parent_index = getParentFoodIndex(mInfos.get(select_position));
        int edgeIndex = parent_index;
        for (int i = 0; i < mInfos.size(); i++) {
            OrderDetail datasBean = mInfos.get(i);
            if (datasBean.getItemType() == 3 && datasBean.getParentIndex() == parent_index) {
                edgeIndex = i;
            }
        }
        return edgeIndex;
    }

    /**
     * 是否包含第三層備註
     *
     * @param orderDetail
     * @return
     */
    private boolean hasChild3Food(OrderDetail orderDetail) {
        if (orderDetail.getItemType() == 3)
            return false;
        for (int i = 0; i < mInfos.size(); i++) {
            if (mInfos.get(i).getItemType() == 3 && mInfos.get(i).getParentIndex() == orderDetail.getMyIndex()) {
                return true;
            }
        }
        return false;
    }

    private boolean hasChildFood(OrderDetail orderDetail) {
        if (orderDetail.getItemType() == 3)
            return false;
        for (int i = 0; i < mInfos.size(); i++) {
            if (mInfos.get(i).getParentIndex() == orderDetail.getMyIndex()) {
                return true;
            }
        }
        return false;
    }

    private int getParentFoodIndex(OrderDetail foodBean) {
        int parent_index = 0;
        if (foodBean.getItemType() == 1) {
            parent_index = foodBean.getMyIndex();
        } else {
            parent_index = foodBean.getParentIndex();
        }
        return parent_index;
    }

    private void setItemNo(OrderDetail foodBean) {
        switch (foodBean.getItemType()) {
            case 1:
//                foodBean.setTopId(foodBean.getProductId());
                break;
            case 2:
            case 3:
                long itemno = -1;
                if (select_position != -1 && select_position < mInfos.size()) {
                    if (mInfos.get(select_position).isIsfood()) {
                        itemno = mInfos.get(select_position).getProductId();
                    } else {
                        if (select_start_position != -1 && select_start_position < mInfos.size()) {
                            itemno = mInfos.get(select_start_position).getProductId();
                        }
                    }
                }
                foodBean.setTopId(itemno);
                break;
            default:
                break;
        }
    }

    /**
     * 设置关联父子项Index
     */
    public void setIndex() {
        // 调整myorder中的parent_index及my_index
        int parent_index = 0;
        int my_index = 0;
        int my_index2 = 0;
        int t = 0;
        // 调整myorder中的顺序位
        // 获取旧项目的最后的seqno不是新项目开始
        int iseqno = 1;
        int isseq = 0;
        int imseq = 0;
        for (int i = 0; i < mInfos.size(); i++) {
            OrderDetail myorder = mInfos.get(i);
            t = t + 1;
            if (myorder != null) {

                switch (myorder.getItemType()) {
                    case 1:
                        parent_index = parent_index + 1;
                        my_index = t;
                        my_index2 = t;
                        myorder.setParentIndex(0);
                        myorder.setMyIndex(t);
                        break;
                    case 2:
                        myorder.setParentIndex(my_index);
                        myorder.setMyIndex(t);
                        my_index2 = t;
                        break;
                    case 3:
                        myorder.setParentIndex(my_index2);
                        myorder.setMyIndex(t);
                        break;
                }
            }
        }
    }

    public void setSelectPosition(int position) {

        if (position == -1)
            return;

        select_position = position;

        int startPos = select_position;
        int endPos = select_position;

        mainStartPos = startPos;
        mainEndPos = endPos;

        if (select_position >= mInfos.size()) {
            select_position = mInfos.size() - 1;
        }
        OrderDetail myorder = mInfos.get(select_position);

        int k = select_position;
        select_full_start_position = k;

        OrderDetail checkOrder = mInfos.get(k);
        while (checkOrder.getItemType() != 1) {
            // checkOrder.selected=3;
            select_full_start_position = k;
            k = k - 1;
            if (k > -1) {
                checkOrder = mInfos.get(k);
                if (checkOrder.getItemType() == 1) {
                    // checkOrder.selected=3;
                    select_full_start_position = k;
                    break;
                }
            } else
                break;
        }

        k = select_position;
        checkOrder = mInfos.get(k);

        if (checkOrder.getItemType() == 1) {
            select_full_end_position = k;
            k = k + 1;
            if (k < mInfos.size()) {
                checkOrder = mInfos.get(k);
            }
        }

        while (checkOrder.getItemType() != 1) {
            checkOrder.setSelected(3);
            select_full_end_position = k;
            k = k + 1;
            if (k < mInfos.size()) {
                checkOrder = mInfos.get(k);
            } else {
                break;
            }
        }

        k = select_position;
        checkOrder = mInfos.get(k);
        if (checkOrder.getComboLevel() == 2) {
            for (int v = k; v >= 0; v--) {
                if (mInfos.get(v).getItemType() < checkOrder.getItemType()) {
                    mainStartPos = v;
                    break;
                }
            }
        } else {
            if (checkOrder.isIsfood()) {
                checkOrder.setSelected(1);
                mainStartPos = k;
                k = k - 1;
                if (k > -1) {
                    checkOrder = mInfos.get(k);
                }
            }

            while (!checkOrder.isIsfood() && checkOrder.getItemType() <= myorder.getItemType()) {
                if (checkOrder.getItemType() < 3 && checkOrder.getItemType() == myorder.getItemType()) {
                    break;
                }
                checkOrder.setSelected(2);
                k = k - 1;
                if (k > -1) {
                    checkOrder = mInfos.get(k);
                    if (checkOrder.getItemType() < 3 && checkOrder.getItemType() == myorder.getItemType()) {
                        break;
                    }
                    if (checkOrder.isIsfood()) {
                        checkOrder.setSelected(1);
                        mainStartPos = k;
                    }
                } else {
                    break;
                }
            }
        }

        k = select_position;
        checkOrder = mInfos.get(k);
        if (checkOrder.isIsfood() || checkOrder.getProp().equals("OrderDetail")) {
            mainEndPos = k;
            k = k + 1;
            if (k < mInfos.size()) {
                checkOrder = mInfos.get(k);
            }
        }
        OrderDetail check_Order = checkOrder;
        while (!checkOrder.isIsfood() || check_Order.getComboLevel() == 2) {
            if (check_Order.getComboLevel() == 2) {
                if (checkOrder.getItemType() == 1) {
                    break;
                }
            } else {
                if (checkOrder.getItemType() < 3 && checkOrder.getItemType() == myorder.getItemType()) {
                    break;
                }
            }
            mainEndPos = k;
            k = k + 1;
            if (k < mInfos.size()) {
                checkOrder = mInfos.get(k);
            } else {
                break;
            }
        }
        for (int v = 0; v < mainStartPos; v++) {
            if (v < mInfos.size()) {
                mInfos.get(v).setSelected(0);
            }
        }
        for (int v = mainEndPos + 1; v < mInfos.size(); v++) {
            mInfos.get(v).setSelected(0);
        }
        if (mainStartPos >= mInfos.size()) {
            mainStartPos = mInfos.size() - 1;
        }
        OrderDetail order = mInfos.get(mainStartPos);
        if (order.getItemType() != 1) {
            for (int v = mainStartPos; v >= 0; v--) {
                if (mInfos.get(v).getItemType() != 1) {
                    mInfos.get(v).setSelected(1);
                } else {
                    mInfos.get(v).setSelected(1);
                    break;
                }
            }
        }
        for (int v = mainStartPos + 1; v < mInfos.size(); v++) {
            if (mInfos.get(v).getItemType() != 1) {
                mInfos.get(v).setSelected(1);
            } else {
                break;
            }
        }

        for (int v = mainStartPos; v <= mainEndPos; v++) {
            if (v < mInfos.size()) {
                mInfos.get(v).setSelected(1);
            }
        }

        mInfos.get(select_position).setSelected(2);
        if (select_position < mainEndPos) {
            for (int v = select_position + 1; v <= mainEndPos; v++) {
                if (v < mInfos.size()) {
                    if (mInfos.get(v).getItemType() <= mInfos.get(select_position).getItemType()) {
                        break;
                    }
                    if (mInfos.get(v).getItemType() != 3) {
                        break;
                    }
                    if (mInfos.get(v).getItemType() == 3) {
                        mInfos.get(v).setSelected(2);
                        ;
                    }
                }
            }
        }
        select_start_position = mainStartPos;
        select_end_position = mainEndPos;

        if(mOnItemClickListener != null){
            mOnItemClickListener.onCurrentOrderDetail(mInfos.get(select_position),select_position);
        }
        notifyDataSetChanged();
    }


    /**
     * 获取食品子项最后一个子项位置
     *
     * @return
     */
    private int getComboEdgeIndex() {
        int parent_index = getParentFoodIndex(mInfos.get(select_position));
        int edgeIndex = parent_index;
        for (int i = 0; i < mInfos.size(); i++) {
            OrderDetail datasBean = mInfos.get(i);
            if (datasBean.getItemType() == 2 && datasBean.getParentIndex() == parent_index) {
                edgeIndex = i;
            }
        }
        return edgeIndex;
    }
    
    public List<OrderDetail> getInfos() {
        return mInfos;
    }

    public int getSelect_position() {
        return select_position;
    }

    public void setSelect_position(int select_position) {
        this.select_position = select_position;
    }

    public void setSelect_end_position(int select_end_position) {
        this.select_end_position = select_end_position;
    }

    public int getSelect_start_position() {
        return select_start_position;
    }

    public int getSelect_end_position() {
        return select_end_position;
    }

    public int getSelect_full_start_position() {
        return select_full_start_position;
    }

    public int getSelect_full_end_position() {
        return select_full_end_position;
    }

    public int getMainStartPos() {
        return mainStartPos;
    }

    public int getMainEndPos() {
        return mainEndPos;
    }

    public OrderDetail getCurrentOrderDetail() {
        if (select_position != -1) {
            return mInfos.get(select_position);
        } else {
            return null;
        }
    }

    public OrderDetail getCurrentMainOrderDetail() {
        if (select_start_position != -1) {
            return mInfos.get(select_start_position);
        } else {
            return null;
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {

        void onItemClick(OrderDetail datasBean, int position);

        void onMealCountDelete(OrderDetail datasBean, int position);

        void onMealCountAdd(OrderDetail datasBean, int position);

        void onCurrentOrderDetail(OrderDetail datasBean, int position);
    }

}
