package com.tencent.mm.plugin.gallery.ui;

import android.content.Context;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.kernel.g;
import com.tencent.mm.plugin.gallery.model.GalleryItem.ImageMediaItem;
import com.tencent.mm.plugin.gallery.model.GalleryItem.MediaItem;
import com.tencent.mm.plugin.gallery.model.GalleryItem.VideoMediaItem;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.mm.ui.base.h;
import com.tencent.mm.ui.gridviewheaders.e;
import com.tencent.ytcommon.BuildConfig;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import org.xwalk.core.XWalkFeature;

public final class a extends BaseAdapter implements e {
    int gLP = 10;
    private SimpleDateFormat kIy = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private Context mContext;
    int mOF = 9;
    com.tencent.mm.plugin.gallery.stub.a mOG = null;
    ArrayList<MediaItem> mOH = new ArrayList();
    ArrayList<MediaItem> mOI = new ArrayList();
    private b mOJ;
    int mOK;
    String mOL;
    LinkedList<a> mOM = new LinkedList();
    boolean mON = false;
    boolean mOO = true;
    private OnClickListener mOP = new OnClickListener() {
        public final void onClick(View view) {
            AppMethodBeat.i(21360);
            if (view.getTag(2131823910) instanceof Integer) {
                Integer num = (Integer) view.getTag(2131823910);
                if (num == null) {
                    ab.e("MicroMsg.AlbumAdapter", "[onClick] null == position!");
                    AppMethodBeat.o(21360);
                    return;
                }
                MediaItem mediaItem = (MediaItem) a.this.mOH.get(num.intValue());
                if (mediaItem == null || bo.isNullOrNil(mediaItem.fPT)) {
                    ab.e("MicroMsg.AlbumAdapter", "[onClick] null == item!");
                    AppMethodBeat.o(21360);
                    return;
                }
                int i;
                boolean z;
                ab.i("MicroMsg.AlbumAdapter", "click Image path:" + mediaItem.fPT);
                if (a.this.mOI.contains(mediaItem)) {
                    a.this.mOI.indexOf(mediaItem);
                    a.this.mOI.remove(mediaItem);
                    i = 1;
                    z = false;
                } else {
                    if (com.tencent.mm.plugin.gallery.model.e.bBZ().hOZ == 3 && mediaItem != null && mediaItem.mMimeType.equalsIgnoreCase("image/gif")) {
                        int cs = com.tencent.mm.a.e.cs(mediaItem.fPT);
                        if (!((com.tencent.mm.plugin.emoji.b.d) g.M(com.tencent.mm.plugin.emoji.b.d.class)).getProvider().Jq(mediaItem.fPT)) {
                            try {
                                a.this.mOG.aK(13459, cs + ",1,,0");
                            } catch (RemoteException e) {
                                ab.printErrStackTrace("MicroMsg.AlbumAdapter", e, BuildConfig.FLAVOR, new Object[0]);
                            }
                            h.bQ(a.this.mContext, a.this.mContext.getString(2131300196));
                            AppMethodBeat.o(21360);
                            return;
                        }
                    }
                    if ((a.this.mOK == 2 || a.this.mOK == 3) && (mediaItem instanceof VideoMediaItem) && !bo.isNullOrNil(a.this.mOL) && a.this.mOL.equals("album_business_bubble_media_by_coordinate") && ((VideoMediaItem) mediaItem).fPW >= (a.this.gLP * XWalkFeature.INTERFACE_GEO_LOCATION_PERMISSION) + 500) {
                        h.bQ(a.this.mContext, a.this.mContext.getResources().getString(2131300214, new Object[]{Integer.valueOf(a.this.gLP)}));
                        AppMethodBeat.o(21360);
                        return;
                    } else if (a.this.mOI.size() < a.this.mOF) {
                        a.this.mOI.add(mediaItem);
                        a.this.mOI.size();
                        i = 0;
                        z = false;
                    } else {
                        i = 1;
                        z = true;
                    }
                }
                if (z) {
                    if (a.this.mOK == 1) {
                        h.bQ(a.this.mContext, a.this.mContext.getResources().getQuantityString(2131361804, a.this.mOF, new Object[]{Integer.valueOf(a.this.mOF)}));
                    } else if (a.this.mOK == 2) {
                        h.bQ(a.this.mContext, a.this.mContext.getResources().getQuantityString(2131361806, a.this.mOF, new Object[]{Integer.valueOf(a.this.mOF)}));
                    } else {
                        h.bQ(a.this.mContext, a.this.mContext.getResources().getQuantityString(2131361805, a.this.mOF, new Object[]{Integer.valueOf(a.this.mOF)}));
                    }
                } else if (a.this.mOJ != null) {
                    a.this.mOJ.W(a.this.mOI.size(), num.intValue(), i);
                }
                if (1 == i) {
                    ((CheckBox) view.getTag(2131823909)).setChecked(false);
                    ((View) view.getTag(2131826568)).setVisibility(0);
                    ((View) view.getTag(2131826568)).setBackgroundResource(2131690176);
                    AppMethodBeat.o(21360);
                    return;
                }
                ((CheckBox) view.getTag(2131823909)).setChecked(true);
                ((View) view.getTag(2131826568)).setVisibility(0);
                ((View) view.getTag(2131826568)).setBackgroundResource(2131690155);
                AppMethodBeat.o(21360);
                return;
            }
            AppMethodBeat.o(21360);
        }
    };

    static class d {
        public ImageView mOW;
        public ImageView mOX;
        public RelativeLayout mOY;
        public TextView mOZ;
        public TextView mPa;
        public ImageView mPb;
        public ImageView mPc;
        public ImageView mPd;
        public CheckBox moo;
        public View mop;

        private d() {
        }

        /* synthetic */ d(byte b) {
            this();
        }
    }

    public final /* synthetic */ Object getItem(int i) {
        AppMethodBeat.i(21385);
        MediaItem ws = ws(i);
        AppMethodBeat.o(21385);
        return ws;
    }

    public a(Context context, b bVar) {
        int i = 0;
        AppMethodBeat.i(21372);
        this.mContext = context;
        this.mOJ = bVar;
        while (i < 32) {
            this.mOH.add(new ImageMediaItem());
            i++;
        }
        AppMethodBeat.o(21372);
    }

    public final void a(a aVar) {
        AppMethodBeat.i(21373);
        if (aVar == null) {
            ab.w("MicroMsg.AlbumAdapter", "addHeader error, header is null");
            AppMethodBeat.o(21373);
            return;
        }
        this.mOM.remove(aVar);
        this.mOM.add(aVar);
        AppMethodBeat.o(21373);
    }

    public final ArrayList<String> bCw() {
        AppMethodBeat.i(21374);
        ArrayList arrayList = new ArrayList();
        Iterator it = this.mOI.iterator();
        while (it.hasNext()) {
            arrayList.add(((MediaItem) it.next()).fPT);
        }
        AppMethodBeat.o(21374);
        return arrayList;
    }

    public final void C(ArrayList<String> arrayList) {
        AppMethodBeat.i(21375);
        ab.d("MicroMsg.AlbumAdapter", "before set selected paths, selected[%s]", new Object[]{this.mOI});
        this.mOI.clear();
        if (arrayList != null) {
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                String str = (String) it.next();
                MediaItem a = MediaItem.a(0, 0, str, str, BuildConfig.FLAVOR);
                if (com.tencent.mm.plugin.gallery.model.e.bCb() != null) {
                    int indexOf = com.tencent.mm.plugin.gallery.model.e.bCb().indexOf(a);
                    if (indexOf >= 0) {
                        a = (MediaItem) com.tencent.mm.plugin.gallery.model.e.bCb().get(indexOf);
                        if (a != null && a.getType() == 2) {
                            this.mOI.add(MediaItem.a(2, 0, str, BuildConfig.FLAVOR, BuildConfig.FLAVOR));
                        }
                    }
                }
                ab.d("MicroMsg.AlbumAdapter", "media item no exist on preview items.");
                this.mOI.add(MediaItem.a(1, 0, str, BuildConfig.FLAVOR, BuildConfig.FLAVOR));
            }
        }
        ab.d("MicroMsg.AlbumAdapter", "after set selected paths, selected[%s]", new Object[]{this.mOI});
        AppMethodBeat.o(21375);
    }

    public final ArrayList<MediaItem> wr(int i) {
        AppMethodBeat.i(21376);
        ArrayList arrayList = new ArrayList();
        Iterator it = this.mOH.iterator();
        while (it.hasNext()) {
            MediaItem mediaItem = (MediaItem) it.next();
            if (mediaItem.getType() == i) {
                arrayList.add(mediaItem);
            }
        }
        AppMethodBeat.o(21376);
        return arrayList;
    }

    public final int getCount() {
        AppMethodBeat.i(21377);
        int size = this.mOM.size() + this.mOH.size();
        AppMethodBeat.o(21377);
        return size;
    }

    public final int getViewTypeCount() {
        AppMethodBeat.i(21378);
        int size = this.mOM.size() + 1;
        AppMethodBeat.o(21378);
        return size;
    }

    public final int getItemViewType(int i) {
        AppMethodBeat.i(21379);
        if (i < this.mOM.size()) {
            AppMethodBeat.o(21379);
            return i;
        }
        i = this.mOM.size();
        AppMethodBeat.o(21379);
        return i;
    }

    public final MediaItem ws(int i) {
        AppMethodBeat.i(21380);
        if (i < this.mOM.size()) {
            ab.i("MicroMsg.AlbumAdapter", "get header, pos[%d]", new Object[]{Integer.valueOf(i)});
            AppMethodBeat.o(21380);
            return null;
        }
        int size = i - this.mOM.size();
        MediaItem imageMediaItem;
        if (size >= this.mOH.size()) {
            ab.w("MicroMsg.AlbumAdapter", "get item error, media items size[%d], adjustPos[%d]", new Object[]{Integer.valueOf(this.mOH.size()), Integer.valueOf(size)});
            imageMediaItem = new ImageMediaItem();
            imageMediaItem.mOl = bo.anU();
            AppMethodBeat.o(21380);
            return imageMediaItem;
        }
        imageMediaItem = (MediaItem) this.mOH.get(size);
        AppMethodBeat.o(21380);
        return imageMediaItem;
    }

    public final long getItemId(int i) {
        return (long) i;
    }

    public final View getView(int i, View view, ViewGroup viewGroup) {
        AppMethodBeat.i(21381);
        if (i < this.mOM.size()) {
            ab.i("MicroMsg.AlbumAdapter", "position[%d], get header view", new Object[]{Integer.valueOf(i)});
            View view2 = ((a) this.mOM.get(i)).getView();
            AppMethodBeat.o(21381);
            return view2;
        }
        String str;
        View inflate;
        d dVar;
        int size = i - this.mOM.size();
        MediaItem mediaItem = (MediaItem) this.mOH.get(size);
        if (view == null || !(view.getTag() instanceof d)) {
            String str2 = "MicroMsg.AlbumAdapter";
            str = "converview is null?[%B]";
            Object[] objArr = new Object[1];
            objArr[0] = Boolean.valueOf(view == null);
            ab.d(str2, str, objArr);
            inflate = LayoutInflater.from(this.mContext).inflate(2130970555, viewGroup, false);
            d dVar2 = new d();
            dVar2.mOW = (ImageView) inflate.findViewById(2131826569);
            dVar2.mOX = (ImageView) inflate.findViewById(2131822355);
            dVar2.mOY = (RelativeLayout) inflate.findViewById(2131825005);
            dVar2.mOZ = (TextView) inflate.findViewById(2131826565);
            dVar2.moo = (CheckBox) inflate.findViewById(2131823909);
            dVar2.mPa = (TextView) inflate.findViewById(2131827238);
            dVar2.mop = inflate.findViewById(2131823910);
            dVar2.mPb = (ImageView) inflate.findViewById(2131826568);
            dVar2.mop.setOnClickListener(this.mOP);
            dVar2.mop.setTag(2131823909, dVar2.moo);
            dVar2.mop.setTag(2131827238, dVar2.mPa);
            dVar2.mop.setTag(2131826568, dVar2.mPb);
            if (com.tencent.mm.plugin.gallery.model.e.bBZ().hOZ == 0 || com.tencent.mm.plugin.gallery.model.e.bBZ().hOZ == 5 || com.tencent.mm.plugin.gallery.model.e.bBZ().hOZ == 10 || com.tencent.mm.plugin.gallery.model.e.bBZ().hOZ == 11 || com.tencent.mm.plugin.gallery.model.e.bBZ().hOZ == 14) {
                dVar2.moo.setVisibility(8);
                dVar2.mPa.setVisibility(8);
                dVar2.mop.setVisibility(8);
                dVar2.mPb.setVisibility(8);
            }
            dVar2.mPc = (ImageView) inflate.findViewById(2131826566);
            dVar2.mPd = (ImageView) inflate.findViewById(2131826567);
            inflate.setTag(dVar2);
            dVar = dVar2;
        } else {
            dVar = (d) view.getTag();
            inflate = view;
        }
        if (mediaItem == null) {
            ab.e("MicroMsg.AlbumAdapter", "get item failed");
            AppMethodBeat.o(21381);
            return inflate;
        }
        dVar.mOW.setVisibility(0);
        if (mediaItem.getType() == 2) {
            dVar.mOY.setVisibility(0);
            c.a(dVar.mOZ, (VideoMediaItem) mediaItem);
        } else {
            dVar.mOY.setVisibility(8);
        }
        if (mediaItem.mMimeType.equalsIgnoreCase("edit")) {
            dVar.mPd.setVisibility(0);
        } else {
            dVar.mPd.setVisibility(8);
        }
        str = mediaItem.Wy();
        String str3 = mediaItem.fPT;
        if (bo.isNullOrNil(str) && bo.isNullOrNil(str3)) {
            ab.e("MicroMsg.AlbumAdapter", "null or nil filepath: ".concat(String.valueOf(size)));
            AppMethodBeat.o(21381);
            return inflate;
        }
        dVar.mop.setTag(2131823910, Integer.valueOf(size));
        int i2 = 2131301951;
        if (mediaItem.getType() == 2) {
            i2 = 2131304275;
        }
        ab.d("MicroMsg.AlbumAdapter", "thumbFilaPath: %s | origFilePath: %s | contentDescription %s", new Object[]{str, str3, viewGroup.getContext().getString(i2, new Object[]{Integer.valueOf((i + 1) - this.mOM.size()), this.kIy.format(new Date(mediaItem.mOl))})});
        dVar.mOX.setContentDescription(r2);
        h.a(dVar.mOX, mediaItem.getType(), str, str3, mediaItem.mOk, -1, new 1(this, dVar.mOW));
        if (this.mON) {
            if (mediaItem.getType() == 2) {
                dVar.moo.setVisibility(8);
                dVar.mPa.setVisibility(8);
                dVar.mop.setVisibility(8);
                dVar.mPb.setVisibility(8);
            } else if (this.mOI.contains(mediaItem)) {
                dVar.moo.setChecked(true);
                TextView textView = dVar.mPa;
                StringBuilder stringBuilder = new StringBuilder();
                if (mediaItem == null) {
                    i2 = -1;
                } else {
                    i2 = this.mOI.indexOf(mediaItem);
                }
                textView.setText(stringBuilder.append(i2).toString());
                dVar.mPb.setBackgroundResource(2131690155);
                dVar.mop.setVisibility(0);
                dVar.moo.setVisibility(0);
                dVar.mPb.setVisibility(0);
            } else {
                dVar.mPa.setVisibility(8);
                dVar.moo.setChecked(false);
                dVar.mPb.setBackgroundResource(2131690176);
                dVar.moo.setVisibility(0);
                dVar.mop.setVisibility(0);
                dVar.mPb.setVisibility(0);
            }
        } else if (this.mOI.contains(mediaItem)) {
            dVar.moo.setChecked(true);
            dVar.mPb.setVisibility(0);
            dVar.mPb.setBackgroundResource(2131690155);
        } else {
            dVar.moo.setChecked(false);
            dVar.mPa.setVisibility(8);
            dVar.mPb.setVisibility(0);
            dVar.mPb.setBackgroundResource(2131690176);
        }
        if ((com.tencent.mm.plugin.gallery.model.e.bBZ().hOZ == 3 || com.tencent.mm.plugin.gallery.model.e.bBZ().hOZ == 11) && mediaItem != null && mediaItem.mMimeType.equalsIgnoreCase("image/gif")) {
            dVar.mPc.setVisibility(0);
        } else {
            dVar.mPc.setVisibility(8);
        }
        AppMethodBeat.o(21381);
        return inflate;
    }

    public final long tw(int i) {
        AppMethodBeat.i(21382);
        if (i < this.mOM.size()) {
            ab.d("MicroMsg.AlbumAdapter", "want to get header view headerId, old pos[%d]", new Object[]{Integer.valueOf(i)});
            i = this.mOM.size();
        }
        ab.v("MicroMsg.AlbumAdapter", "getHeaderId, adjust pos[%d], date[%d] date[%s], headerID[%d]", new Object[]{Integer.valueOf(i), Long.valueOf(ws(i).mOl), r1, Long.valueOf(com.tencent.mm.ui.gridviewheaders.a.dJg().b(new Date(ws(i).mOl)))});
        AppMethodBeat.o(21382);
        return com.tencent.mm.ui.gridviewheaders.a.dJg().b(new Date(ws(i).mOl));
    }

    public final String wt(int i) {
        AppMethodBeat.i(21383);
        if (i < this.mOM.size()) {
            ab.d("MicroMsg.AlbumAdapter", "want to get header view headerId, old pos[%d]", new Object[]{Integer.valueOf(i)});
            i = this.mOM.size();
        }
        MediaItem ws = ws(i);
        String a;
        if (bo.isNullOrNil(ws.mOn)) {
            a = com.tencent.mm.ui.gridviewheaders.a.dJg().a(new Date(ws.mOl), this.mContext);
            AppMethodBeat.o(21383);
            return a;
        }
        a = ws.mOn;
        AppMethodBeat.o(21383);
        return a;
    }

    public final View a(int i, View view, ViewGroup viewGroup) {
        View textView;
        AppMethodBeat.i(21384);
        if (i < this.mOM.size()) {
            ab.d("MicroMsg.AlbumAdapter", "want to get header view headerId, old pos[%d]", new Object[]{Integer.valueOf(i)});
            i = this.mOM.size();
        }
        if (view == null || view.getParent() != null) {
            LayoutParams layoutParams = new LayoutParams(-1, -2);
            textView = new TextView(this.mContext);
            textView.setBackgroundResource(2130839162);
            int dimensionPixelSize = this.mContext.getResources().getDimensionPixelSize(2131427858);
            int dimensionPixelSize2 = this.mContext.getResources().getDimensionPixelSize(2131427858);
            textView.setPadding(dimensionPixelSize2, dimensionPixelSize, dimensionPixelSize2, dimensionPixelSize);
            textView.setTextColor(this.mContext.getResources().getColor(2131690075));
            textView.setTextSize(0, (float) this.mContext.getResources().getDimensionPixelSize(2131427762));
            textView.setTypeface(null, 1);
            textView.setLayoutParams(layoutParams);
        } else {
            textView = view;
        }
        ab.v("MicroMsg.AlbumAdapter", "getHeaderView, adjust pos[%d], date[%d] date[%s], headerStr[%s]", new Object[]{Integer.valueOf(i), Long.valueOf(r0.mOl), new Date(ws(i).mOl), com.tencent.mm.ui.gridviewheaders.a.dJg().a(new Date(ws(i).mOl), this.mContext)});
        ((TextView) textView).setText(r3);
        AppMethodBeat.o(21384);
        return textView;
    }
}
