package com.lwkandroid.imagepicker.ui.grid.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lwkandroid.imagepicker.R;
import com.lwkandroid.imagepicker.base.adapter.IImagePickerItemView;
import com.lwkandroid.imagepicker.base.adapter.ImagePickerViewHolder;
import com.lwkandroid.imagepicker.data.ImageContants;
import com.lwkandroid.imagepicker.data.ImageBean;
import com.lwkandroid.imagepicker.data.ImageDataModel;
import com.lwkandroid.imagepicker.data.ImagePickType;
import com.lwkandroid.imagepicker.data.ImagePickerOptions;
import com.lwkandroid.imagepicker.ui.grid.view.IImageDataView;

/**
 * Created by LWK
 * TODO 显示图片的GridItem
 */
public class ImageContentItemView implements IImagePickerItemView<ImageBean>
{
    private IImageDataView mViewImpl;
    private ImagePickerOptions mOptions;
    private ImageDataAdapter mAdapter;

    public ImageContentItemView(IImageDataView viewImpl, ImageDataAdapter adapter)
    {
        this.mViewImpl = viewImpl;
        this.mOptions = mViewImpl.getOptions();
        this.mAdapter = adapter;
    }

    @Override
    public int getItemViewLayoutId()
    {
        return R.layout.layout_image_data_content_listitem;
    }

    @Override
    public boolean isForViewType(ImageBean item, int position)
    {
        return mOptions != null && (!mOptions.isNeedCamera() || (mOptions.isNeedCamera() && position != 0));
    }

    @Override
    public void setData(ImagePickerViewHolder holder, final ImageBean imageBean, final int position, ViewGroup parent)
    {
        ImageView imgContent = holder.findView(R.id.img_imagepicker_grid_content);
        View viewIndicator = holder.findView(R.id.ck_imagepicker_grid_content);

        //显示UI
        if (imageBean != null)
            ImageDataModel.getInstance().getDisplayer()
                    .display(holder.getContext(), imageBean.getImagePath(), imgContent
                            , R.drawable.glide_default_picture, R.drawable.glide_default_picture
                            , ImageContants.DISPLAY_THUMB_SIZE, ImageContants.DISPLAY_THUMB_SIZE);
        imgContent.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (mViewImpl != null)
                    mViewImpl.onImageClicked(imageBean, position);
            }
        });

        if (mOptions.getType() == ImagePickType.SINGLE)
        {
            viewIndicator.setVisibility(View.GONE);
        } else
        {
            viewIndicator.setVisibility(View.VISIBLE);
            if (ImageDataModel.getInstance().hasDataInResult(imageBean))
                viewIndicator.setBackgroundResource(R.drawable.ck_imagepicker_grid_selected);
            else
                viewIndicator.setBackgroundResource(R.drawable.ck_imagepicker_grid_normal);

            viewIndicator.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int curNum = ImageDataModel.getInstance().getResultNum();
                    if (curNum == mOptions.getMaxNum())
                    {
                        mViewImpl.warningMaxNum();
                        return;
                    } else
                    {
                        if (ImageDataModel.getInstance().hasDataInResult(imageBean))
                            ImageDataModel.getInstance().delDataFromResult(imageBean);
                        else
                            ImageDataModel.getInstance().addDataToResult(imageBean);
                        mAdapter.notifyDataSetChanged();
                        mViewImpl.onSelectNumChanged(ImageDataModel.getInstance().getResultNum());
                    }
                }
            });
        }
    }
}

@@ -2677,36 +2677,59 @@
 );%0D%0A            
-if (
+final boolean isSelected = 
 ImageDataModel.g
@@ -2759,32 +2759,61 @@
 esult(imageBean)
+;%0D%0A            if (isSelected
 )%0D%0A             
@@ -3211,228 +3211,286 @@
    i
-nt curNum = ImageDataModel.getInstance().getResultNum();%0D%0A                    if (curNum == mOptions.getMaxNum())%0D%0A                    %7B%0D%0A                        mViewImpl.warningMaxNum();%0D%0A                        return
+f (isSelected)%0D%0A                    %7B%0D%0A                        ImageDataModel.getInstance().delDataFromResult(imageBean);%0D%0A                        mAdapter.notifyDataSetChanged();%0D%0A                        mViewImpl.onSelectNumChanged(ImageDataModel.getInstance().getResultNum())
 ;%0D%0A 
@@ -3600,33 +3600,45 @@
 e().
-hasDataInResult(imageBean
+getResultNum() == mOptions.getMaxNum(
 ))%0D%0A
@@ -3640,32 +3640,48 @@
 ())%0D%0A           
+  
+           %7B%0D%0A
                 
@@ -3685,64 +3685,43 @@
     
-ImageDataModel.getInstance().delDataFromResult(imageBean
+           mViewImpl.warningMaxNum(
 );%0D%0A
@@ -3735,39 +3735,68 @@
                 
+ %7D
  else%0D%0A
+                        %7B%0D%0A
                 
@@ -3881,32 +3881,36 @@
                 
+    
 mAdapter.notifyD
@@ -3919,32 +3919,36 @@
 aSetChanged();%0D%0A
+    
                 
@@ -4023,32 +4023,59 @@
 tResultNum());%0D%0A
+                        %7D%0D%0A
                 

fix bug