package com.jahirfiquitiva.paperboard.fragments;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jahirfiquitiva.dashboardsample.R;

import java.util.ArrayList;

public class IconsFragment extends Fragment {

    private Context context;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.icons_grid, container, false);

        context = getActivity();
        GridView gridview = (GridView) view.findViewById(R.id.icons_grid);
        final IconAdapter icAdapter = new IconAdapter();
        gridview.setAdapter(icAdapter);
        return view;

    }

    public static IconsFragment newInstance(int iconsArray)
    {
        IconsFragment fragment = new IconsFragment();
        Bundle args = new Bundle();
        args.putInt("iconsArrayId", iconsArray);
        fragment.setArguments(args);
        return fragment;
    }

    private class IconAdapter extends BaseAdapter {
        private ArrayList<Integer> mThumbs;

        public IconAdapter() {
            loadIcon();
        }

        @Override
        public int getCount() {
            return mThumbs.size();
        }

        @Override
        public Object getItem(int position) {
            return mThumbs.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            View icono = convertView;
            IconsHolder holder;
            Animation anim = AnimationUtils.loadAnimation(context, R.anim.fade_in);

            if (icono == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                icono = inflater.inflate(R.layout.icon, parent, false);
                View dialogIconView = inflater.inflate(R.layout.dialog_icon, null, false);
                icono.setLayoutParams(new GridView.LayoutParams(120, 120));
                holder = new IconsHolder(icono, dialogIconView);
                icono.setTag(holder);
            } else {
                holder = (IconsHolder) icono.getTag();

            }

            holder.icon.startAnimation(anim);
            holder.icon.setImageResource(mThumbs.get(position));
            final IconsHolder finalHolder = holder;
            holder.icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finalHolder.dialogIcon.setImageResource(mThumbs.get(position));
                    new MaterialDialog.Builder(getActivity())
                        .customView(finalHolder.dialog, false)
                        .positiveText(R.string.close)
                        .show();
                }
            });

            return icono;
        }

        class IconsHolder {
            ImageView icon;
            View dialog;
            ImageView dialogIcon;
            IconsHolder(View v, View dialog) {
                icon = (ImageView) v.findViewById(R.id.icon_img);
                this.dialog = dialog;
                dialogIcon = (ImageView) dialog.findViewById(R.id.dialogicon);

            }
        }

        private void loadIcon() {
            mThumbs = new ArrayList<>();

            final Resources resources = getResources();
            final String packageName = getActivity().getApplication().getPackageName();
            addIcon(resources, packageName, getArguments().getInt("iconsArrayId", 0));

        }

        private void addIcon(Resources resources, String packageName, int list) {
            final String[] extras = resources.getStringArray(list);
            for (String extra : extras) {
                int res = resources.getIdentifier(extra, "drawable", packageName);
                if (res != 0) {
                    final int thumbRes = resources.getIdentifier(extra, "drawable", packageName);
                    if (thumbRes != 0) {
                        mThumbs.add(thumbRes);
                    }
                }
            }
        }

    }


}
@@ -2353,99 +2353,8 @@
 e);%0A
-                View dialogIconView = inflater.inflate(R.layout.dialog_icon, null, false);%0A
     
@@ -2475,24 +2475,8 @@
 cono
-, dialogIconView
 );%0A 
@@ -2719,60 +2719,8 @@
 ));%0A
-            final IconsHolder finalHolder = holder;%0A
     
@@ -2879,28 +2879,216 @@
         
-finalHolder.
+View dialogIconView = View.inflate(getActivity(), R.layout.dialog_icon, null);%0A                    ImageView dialogIcon = (ImageView) dialogIconView.findViewById(R.id.dialogicon);%0A                    
 dialogIc
@@ -3233,26 +3233,22 @@
 iew(
-finalHolder.dialog
+dialogIconView
 , fa
@@ -3471,67 +3471,8 @@
 on;%0A
-            View dialog;%0A            ImageView dialogIcon;%0A
     
@@ -3501,21 +3501,8 @@
 ew v
-, View dialog
 ) %7B%0A
@@ -3571,126 +3571,8 @@
 g);%0A
-                this.dialog = dialog;%0A                dialogIcon = (ImageView) dialog.findViewById(R.id.dialogicon);%0A%0A
     

Fix crash opening same icon dialog more than once