package com.tencent.mm.plugin.sns.ui.previewimageview;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.plugin.sns.data.i;
import com.tencent.mm.plugin.sns.model.af;
import com.tencent.mm.plugin.sns.storage.s;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.ytcommon.BuildConfig;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public final class c extends b {
    private int rKL;
    private boolean rKM;
    private boolean rKN;
    a rKO;
    private int rKP = 0;
    private HashMap<String, Bitmap> rmh = new HashMap();
    boolean rmk;

    public c(Context context, List<?> list, boolean z, a aVar) {
        super(context);
        AppMethodBeat.i(40306);
        super.dy(dz(list));
        this.rKL = 9;
        this.rKM = z;
        this.rKO = aVar;
        cuY();
        cuZ();
        AppMethodBeat.o(40306);
    }

    private List<d> dz(List<?> list) {
        AppMethodBeat.i(40307);
        ArrayList arrayList = new ArrayList(list.size());
        int i = 0;
        for (Object next : list) {
            d dVar = new d(this, (byte) 0);
            dVar.data = next;
            int i2 = i + 1;
            dVar.id = i;
            arrayList.add(dVar);
            i = i2;
        }
        AppMethodBeat.o(40307);
        return arrayList;
    }

    public final void cuZ() {
        AppMethodBeat.i(40309);
        ab.v("DynamicGridAdapter", "showAddImg %s, getCount %d, getHeaderCount %d, maxShowCount %d， showing %s", new Object[]{Boolean.valueOf(this.rKM), Integer.valueOf(getCount()), Integer.valueOf(this.rKK), Integer.valueOf(this.rKL), Boolean.valueOf(this.rKN)});
        if (!this.rKM || cva() >= this.rKL) {
            this.rKN = false;
        } else if (!this.rKN) {
            this.rKN = true;
            add(BuildConfig.FLAVOR);
            AppMethodBeat.o(40309);
            return;
        }
        AppMethodBeat.o(40309);
    }

    public final void clear() {
        AppMethodBeat.i(40310);
        super.clear();
        this.rKN = false;
        AppMethodBeat.o(40310);
    }

    public final void dy(List<?> list) {
        AppMethodBeat.i(40311);
        super.dy(dz(list));
        cuY();
        cuZ();
        AppMethodBeat.o(40311);
    }

    public final int cva() {
        AppMethodBeat.i(40312);
        int count = (getCount() - this.rKK) - (this.rKN ? 1 : 0);
        AppMethodBeat.o(40312);
        return count;
    }

    public final View getView(int i, View view, ViewGroup viewGroup) {
        b bVar;
        int screenWidth;
        AppMethodBeat.i(40313);
        if (view == null) {
            view = LayoutInflater.from(getContext()).inflate(2130970793, viewGroup, false);
            bVar = new b(this, view, (byte) 0);
            view.setTag(2131820631, bVar);
            ImageView imageView = bVar.cAw;
            if (this.rKP == 0) {
                af.cnu();
                screenWidth = s.getScreenWidth();
                if (screenWidth != 0) {
                    this.rKP = ((screenWidth - (this.mContext.getResources().getDimensionPixelSize(2131428681) * 4)) - (this.mContext.getResources().getDimensionPixelSize(2131427779) * 2)) / 3;
                }
            }
            LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();
            layoutParams.width = this.rKP;
            layoutParams.height = this.rKP;
            imageView.setLayoutParams(layoutParams);
        } else {
            bVar = (b) view.getTag(2131820631);
        }
        String obj = getItem(i).toString();
        ImageView imageView2 = bVar.cAw;
        if (TextUtils.isEmpty(obj)) {
            imageView2.setBackgroundResource(2131690503);
            imageView2.setImageResource(2131230780);
            imageView2.setContentDescription(bVar.cAw.getContext().getString(2131303591));
            screenWidth = (bVar.rKQ.rKP * 35) / 100;
            imageView2.setPadding(screenWidth, screenWidth, screenWidth, screenWidth);
            if (i > 0) {
                bVar.view.setTag(Integer.valueOf(-1));
            } else if (i < bVar.rKQ.rKK) {
                bVar.view.setTag(Integer.valueOf(Integer.MAX_VALUE));
            }
        } else {
            bVar.view.setTag(Integer.valueOf(i - bVar.rKQ.rKK));
            imageView2.setBackgroundDrawable(null);
            imageView2.setTag(obj);
            imageView2.setContentDescription(bVar.cAw.getContext().getString(2131303660));
            imageView2.setPadding(0, 0, 0, 0);
            Bitmap bitmap = (Bitmap) bVar.rKQ.rmh.get(obj);
            if (i.B(bitmap)) {
                imageView2.setImageBitmap(bitmap);
            } else {
                new c(bVar.rKQ, imageView2, obj).u(new String[]{BuildConfig.FLAVOR});
            }
        }
        if (i < this.rKK) {
            bVar.cAw.setVisibility(4);
        } else {
            bVar.cAw.setVisibility(0);
            view.setVisibility(0);
        }
        AppMethodBeat.o(40313);
        return view;
    }

    public final int getItemViewType(int i) {
        AppMethodBeat.i(40314);
        if (TextUtils.isEmpty(getItem(i).toString())) {
            AppMethodBeat.o(40314);
            return 1;
        }
        AppMethodBeat.o(40314);
        return 0;
    }

    public final int getViewTypeCount() {
        return 2;
    }

    public final void fB(int i, int i2) {
        AppMethodBeat.i(40317);
        super.fB(i, i2);
        if (this.rKO != null) {
            this.rKO.fu(i - this.rKK, i2 - this.rKK);
        }
        AppMethodBeat.o(40317);
    }

    private void cuY() {
        AppMethodBeat.i(40308);
        for (int i = 0; i < this.rKK; i++) {
            String str = BuildConfig.FLAVOR;
            d dVar = new d(this, (byte) 0);
            dVar.data = str;
            dVar.id = getCount();
            dVar.id = getCount();
            add(i, dVar);
        }
        AppMethodBeat.o(40308);
    }

    public final boolean Es(int i) {
        AppMethodBeat.i(40315);
        if (i < this.rKK) {
            AppMethodBeat.o(40315);
            return false;
        } else if (!this.rKN) {
            boolean Es = super.Es(i);
            AppMethodBeat.o(40315);
            return Es;
        } else if (i != getCount() - 1) {
            AppMethodBeat.o(40315);
            return true;
        } else {
            AppMethodBeat.o(40315);
            return false;
        }
    }

    public final boolean Et(int i) {
        AppMethodBeat.i(40316);
        if (i < this.rKK) {
            AppMethodBeat.o(40316);
            return false;
        } else if (!this.rKN) {
            boolean Et = super.Et(i);
            AppMethodBeat.o(40316);
            return Et;
        } else if (i != getCount() - 1) {
            AppMethodBeat.o(40316);
            return true;
        } else {
            AppMethodBeat.o(40316);
            return false;
        }
    }
}
