package com.example.stream.eb.main.shopping;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.stream.core.delegates.bottom.BottomPageDelegate;
import com.example.stream.core.network.RestClient;
import com.example.stream.core.network.callback.ISuccess;
import com.example.stream.core.ui.recycler.ComplexItemEntity;
import com.example.stream.eb.R;
import com.example.stream.eb.R2;
import com.joanzapata.iconify.widget.IconTextView;
import com.tencent.mm.opensdk.utils.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by StReaM on 8/30/2017.
 */

public class ShopCartDelegate extends BottomPageDelegate implements ISuccess {
    private ShopCartAdapter mAdapter = null;
    private int mCurrentCount = 0;
    private int mTotalCount = 0;

    @BindView(R2.id.rv_shop_cart)
    RecyclerView mRecyclerView = null;
    @BindView(R2.id.icon_select_all)
    IconTextView mIconSelectAll = null;

    // 对整个 recyclerView 设置全选
    @OnClick(R2.id.icon_select_all)
    void onClickSelectAll(View view){
        final int tag = (int)mIconSelectAll.getTag();
        if (tag == 0) {
            mIconSelectAll
                    .setTextColor(ContextCompat.getColor(getContext(), R.color.item_choose));
            mIconSelectAll.setTag(1);
            mAdapter.setSelectAll(true);
            mAdapter.notifyItemRangeChanged(0, mAdapter.getItemCount());
        } else {
            mIconSelectAll.setTextColor(Color.GRAY);
            mIconSelectAll.setTag(0);
            mAdapter.setSelectAll(false);
            mAdapter.notifyItemRangeChanged(0, mAdapter.getItemCount());
        }
    }

    @OnClick(R2.id.tv_delete_product)
    void onClickDelete(){
        final List<ComplexItemEntity> dataList = mAdapter.getData();
        final List<ComplexItemEntity> deleteEntities = new ArrayList<>();
        for (ComplexItemEntity entity: dataList) {
            final boolean selected = entity.getField(ShopCartItemFields.SELECTED);
            if (selected) {
                deleteEntities.add(entity);
            }
        }

        for (ComplexItemEntity entity: deleteEntities) {
            int removePosition;
            final int entityPosition = entity.getField(ShopCartItemFields.POSITION);
            if (entityPosition > mCurrentCount - 1) {
                removePosition = entityPosition - (mTotalCount - mCurrentCount);
            } else {
                removePosition = entityPosition;
            }

            if (removePosition <= mAdapter.getItemCount()) {
                mAdapter.remove(removePosition);
                mCurrentCount = mAdapter.getItemCount();
                mAdapter.notifyItemRangeChanged(removePosition, mAdapter.getItemCount());
            }
        }
    }

    @OnClick(R2.id.tv_clear_cart)
    void onClearCart() {
        mAdapter.getData().clear();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_shop_cart;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mIconSelectAll.setTag(0);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        RestClient.Builder()
                .url("shop_cart.php")
                .success(this)
                .build()
                .get();
    }

    @Override
    public void onSuccess(String response) {
        final ArrayList<ComplexItemEntity> list = new ShopCartDataConverter()
                        .setJsonData(response)
                        .convert();
        mAdapter = new ShopCartAdapter(list);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }
}

@@ -995,76 +995,8 @@
 ull;
-%0A    private int mCurrentCount = 0;%0A    private int mTotalCount = 0;
 %0A%0A  
@@ -1245,16 +1245,17 @@
 ew view)
+ 
 %7B%0A      
@@ -1277,16 +1277,17 @@
  = (int)
+ 
 mIconSel
@@ -1902,17 +1902,42 @@
 Delete()
-%7B
+ %7B%0A        // %E6%B2%A1%E6%B3%95%E4%B8%80%E8%BE%B9%E8%BF%AD%E4%BB%A3%E4%B8%80%E8%BE%B9%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0
 %0A       
@@ -2021,76 +2021,145 @@
 ist%3C
-ComplexItemEntity%3E deleteEntities = new ArrayList%3C%3E();%0A        for (
+Integer%3E deleteIndex = new ArrayList%3C%3E();%0A        final int size = dataList.size();%0A        for (int i = 0; i %3C size; i++) %7B%0A            
 Comp
@@ -2178,17 +2178,18 @@
 y entity
-:
+ =
  dataLis
@@ -2189,19 +2189,24 @@
 dataList
-) %7B
+.get(i);
 %0A       
@@ -2335,27 +2335,19 @@
 lete
-Entities.add(entity
+Index.add(i
 );%0A 
@@ -2361,33 +2361,32 @@
     %7D%0A        %7D%0A
-%0A
         for (Com
@@ -2382,559 +2382,185 @@
    f
-or (ComplexItemEntity entity: deleteEntities) %7B%0A            int removePosition;%0A            final int entityPosition = entity.getField(ShopCartItemFields.POSITION);%0A            if (entityPosition %3E mCurrentCount - 1) %7B%0A                removePosition = entityPosition - (mTotalCount - mCurrentCount);%0A            %7D else %7B%0A                removePosition = entityPosition;%0A            %7D%0A%0A            if (removePosition %3C= mAdapter.getItemCount()) %7B%0A                mAdapter.remove(removePosition);%0A                mCurrentCount = mAdapter.getItemCount();
+inal int j = deleteIndex.size();%0A        for (Integer i : deleteIndex) %7B%0A            dataList.remove((j-i-1)); // reverse, prevent from IndexOutOfBoundsException while deleteing
 %0A   
@@ -2556,32 +2556,34 @@
 leteing%0A        
+%7D%0A
         mAdapter
@@ -2593,90 +2593,25 @@
 tify
-ItemRangeChanged(removePosition, mAdapter.getItemCount());%0A            %7D%0A        %7D
+DataSetChanged();
 %0A   
@@ -3427,32 +3427,24 @@
             
-        
 .setJsonData
@@ -3454,24 +3454,16 @@
 sponse)%0A
-        
         

add a elegant way of deleting shop cart