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

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.plugin.card.d.d;
import com.tencent.mm.plugin.card.d.l;
import com.tencent.mm.plugin.card.d.m;
import com.tencent.mm.plugin.card.model.am;
import com.tencent.mm.plugin.card.model.g;
import com.tencent.mm.plugin.card.model.g.b;
import com.tencent.mm.plugin.card.model.h;
import com.tencent.mm.protocal.protobuf.nm;
import com.tencent.mm.protocal.protobuf.nz;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.mm.ui.MMActivity;
import com.tencent.mm.ui.p;
import com.tencent.mm.ui.v;
import com.tencent.ytcommon.BuildConfig;
import java.util.List;

public class CardNewMsgUI extends MMActivity implements com.tencent.mm.plugin.card.b.k.a {
    private View Ek;
    OnClickListener gKK = new 9(this);
    private a kmR;
    private View kmS;
    private View kmT = null;
    private boolean kmU = false;
    private ListView mListView;

    class a extends p<g> {
        private int enb = this.kmW;
        private int kmW = 10;

        public final /* synthetic */ Object a(Object obj, Cursor cursor) {
            AppMethodBeat.i(88609);
            g gVar = (g) obj;
            if (gVar == null) {
                gVar = new g();
            }
            gVar.d(cursor);
            AppMethodBeat.o(88609);
            return gVar;
        }

        public a() {
            super(CardNewMsgUI.this, new g());
            AppMethodBeat.i(88603);
            pN(true);
            AppMethodBeat.o(88603);
        }

        public final void KC() {
            AppMethodBeat.i(88604);
            if (am.baY().baS() > 0) {
                setCursor(am.baY().bSd.rawQuery("select * from CardMsgInfo where read_state = ?  order by time desc", new String[]{"1"}));
            } else {
                this.enb = am.baY().getCount();
                h baY = am.baY();
                setCursor(baY.bSd.rawQuery("select * from CardMsgInfo order by time desc LIMIT ".concat(String.valueOf(this.kmW)), null));
            }
            notifyDataSetChanged();
            AppMethodBeat.o(88604);
        }

        public final void KD() {
            AppMethodBeat.i(88605);
            bIf();
            KC();
            AppMethodBeat.o(88605);
        }

        public final boolean bdn() {
            return this.kmW >= this.enb;
        }

        public final int bdo() {
            AppMethodBeat.i(88606);
            if (bdn()) {
                if (CardNewMsgUI.this.kmT.getParent() != null) {
                    CardNewMsgUI.this.mListView.removeFooterView(CardNewMsgUI.this.kmT);
                }
                AppMethodBeat.o(88606);
                return 0;
            }
            this.kmW += 10;
            if (this.kmW <= this.enb) {
                AppMethodBeat.o(88606);
                return 10;
            }
            this.kmW = this.enb;
            int i = this.enb % 10;
            AppMethodBeat.o(88606);
            return i;
        }

        public final View getView(int i, View view, ViewGroup viewGroup) {
            b bVar;
            AppMethodBeat.i(88607);
            g gVar = (g) getItem(i);
            if (view == null) {
                view = View.inflate(CardNewMsgUI.this.mController.ylL, 2130968956, null);
                b bVar2 = new b(CardNewMsgUI.this, (byte) 0);
                bVar2.knb = (ImageView) view.findViewById(2131822233);
                bVar2.knc = view.findViewById(2131822208);
                bVar2.knd = (ImageView) view.findViewById(2131822209);
                bVar2.kne = (TextView) view.findViewById(2131821185);
                bVar2.knf = (TextView) view.findViewById(2131820988);
                bVar2.kng = (TextView) view.findViewById(2131822234);
                bVar2.knh = (TextView) view.findViewById(2131822235);
                bVar2.kni = (TextView) view.findViewById(2131822238);
                bVar2.knj = view.findViewById(2131822239);
                bVar2.knk = (TextView) view.findViewById(2131822240);
                bVar2.knl = (LinearLayout) view.findViewById(2131822237);
                bVar2.knm = (LinearLayout) view.findViewById(2131822236);
                view.setTag(bVar2);
                bVar = bVar2;
            } else {
                bVar = (b) view.getTag();
            }
            bVar.kne.setText(gVar.field_title);
            bVar.knf.setText(com.tencent.mm.pluginsdk.f.h.c(CardNewMsgUI.this, ((long) gVar.field_time) * 1000, true));
            bVar.kng.setText(gVar.field_description);
            int dimensionPixelSize = CardNewMsgUI.this.getResources().getDimensionPixelSize(2131428187);
            ab.d("MicroMsg.CardNewMsgUI", "CardNewMsgUI getView () position : " + i + " logo_url :  " + gVar.field_logo_url);
            if (TextUtils.isEmpty(gVar.field_logo_color)) {
                bVar.knc.setVisibility(8);
                bVar.knb.setVisibility(0);
                bVar.knb.setImageResource(2130839644);
                m.a(bVar.knb, gVar.field_logo_url, dimensionPixelSize, 2130839644, true);
            } else {
                ab.d("MicroMsg.CardNewMsgUI", "CardNewMsgUI getView () position : " + i + " field_logo_color :  " + gVar.field_logo_color);
                bVar.knc.setVisibility(0);
                bVar.knb.setVisibility(8);
                if (TextUtils.isEmpty(gVar.field_logo_url)) {
                    m.a(bVar.knd, 2130838129, l.Hn(gVar.field_logo_color));
                } else {
                    m.a(CardNewMsgUI.this, bVar.knd, gVar.field_logo_url, dimensionPixelSize, l.Hn(gVar.field_logo_color));
                }
            }
            nm baM = gVar.baM();
            if (baM == null || TextUtils.isEmpty(baM.text)) {
                bVar.knh.setVisibility(8);
            } else {
                bVar.knh.setText(baM.text);
                bVar.knh.setTag(gVar);
                bVar.knh.setVisibility(0);
                bVar.knh.setOnClickListener(CardNewMsgUI.this.gKK);
            }
            nz baN = gVar.baN();
            if (baN == null || TextUtils.isEmpty(baN.text)) {
                bVar.knj.setVisibility(8);
                bVar.kni.setVisibility(8);
            } else {
                bVar.knk.setText(baN.text);
                bVar.knj.setVisibility(0);
                bVar.knj.setOnClickListener(CardNewMsgUI.this.gKK);
                bVar.knj.setTag(gVar);
                bVar.kni.setVisibility(0);
            }
            gVar.baO();
            gVar.baP();
            a(gVar.kdv, gVar.kdw, bVar, gVar);
            AppMethodBeat.o(88607);
            return view;
        }

        private void a(List<com.tencent.mm.plugin.card.model.g.a> list, List<b> list2, b bVar, g gVar) {
            AppMethodBeat.i(88608);
            bVar.knl.removeAllViews();
            if ((list == null || list.size() == 0) && (list2 == null || list2.size() == 0)) {
                bVar.knm.setVisibility(8);
                AppMethodBeat.o(88608);
                return;
            }
            int i;
            View inflate;
            Button button;
            if (!bo.ek(list)) {
                for (i = 0; i < list.size(); i++) {
                    com.tencent.mm.plugin.card.model.g.a aVar = (com.tencent.mm.plugin.card.model.g.a) list.get(i);
                    inflate = View.inflate(CardNewMsgUI.this.mController.ylL, 2130968889, null);
                    if (i == 0) {
                        inflate.findViewById(2131821908).setVisibility(8);
                    }
                    ((TextView) inflate.findViewById(2131821909)).setText(aVar.title);
                    ((TextView) inflate.findViewById(2131821910)).setText(aVar.jcF);
                    button = (Button) inflate.findViewById(2131821911);
                    button.setOnClickListener(new 1(this, aVar, gVar));
                    if (aVar.kdC == 0) {
                        button.setText(2131297788);
                    } else if (aVar.kdC == 1) {
                        button.setText(2131297789);
                    }
                    bVar.knl.addView(inflate);
                }
            }
            if (!bo.ek(list2)) {
                for (i = 0; i < list2.size(); i++) {
                    b bVar2 = (b) list2.get(i);
                    inflate = View.inflate(CardNewMsgUI.this.mController.ylL, 2130968889, null);
                    if (i == 0 && bo.ek(list)) {
                        inflate.findViewById(2131821908).setVisibility(8);
                    }
                    ((TextView) inflate.findViewById(2131821909)).setText(bVar2.title);
                    ((TextView) inflate.findViewById(2131821910)).setText(bVar2.description);
                    button = (Button) inflate.findViewById(2131821911);
                    button.setOnClickListener(new 2(this, bVar2));
                    button.setText(bVar2.kdE);
                    bVar.knl.addView(inflate);
                }
            }
            bVar.knm.setVisibility(0);
            AppMethodBeat.o(88608);
        }
    }

    public void onWindowFocusChanged(boolean z) {
        super.onWindowFocusChanged(z);
        AppMethodBeat.at(this, z);
    }

    public CardNewMsgUI() {
        AppMethodBeat.i(88610);
        AppMethodBeat.o(88610);
    }

    static /* synthetic */ void g(CardNewMsgUI cardNewMsgUI) {
        AppMethodBeat.i(88619);
        cardNewMsgUI.bbB();
        AppMethodBeat.o(88619);
    }

    public final int getLayoutId() {
        return 2130968958;
    }

    public void onCreate(Bundle bundle) {
        AppMethodBeat.i(88611);
        super.onCreate(bundle);
        initView();
        am.bba().a(this);
        am.bba().baJ();
        AppMethodBeat.o(88611);
    }

    public void onDestroy() {
        AppMethodBeat.i(88612);
        this.kmR.bIf();
        am.bba().b(this);
        if (am.baY().baS() > 0) {
            am.baY().baT();
        }
        super.onDestroy();
        AppMethodBeat.o(88612);
    }

    public final void initView() {
        AppMethodBeat.i(88613);
        setMMTitle(2131297930);
        setBackBtn(new 1(this));
        this.mListView = (ListView) findViewById(2131822244);
        this.kmR = new a();
        this.mListView.setAdapter(this.kmR);
        this.mListView.setOnItemClickListener(new 2(this));
        this.mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
            public final boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long j) {
                AppMethodBeat.i(88594);
                CardNewMsgUI.b(CardNewMsgUI.this, i);
                AppMethodBeat.o(88594);
                return true;
            }
        });
        this.mListView.setOnScrollListener(new OnScrollListener() {
            public final void onScrollStateChanged(AbsListView absListView, int i) {
                AppMethodBeat.i(88595);
                if (!CardNewMsgUI.this.kmU) {
                    AppMethodBeat.o(88595);
                } else if (absListView.getLastVisiblePosition() == absListView.getCount() - 1) {
                    if (am.baY().baS() > 0) {
                        am.baY().baT();
                    } else {
                        CardNewMsgUI.this.kmR.bdo();
                    }
                    CardNewMsgUI.this.kmR.a(null, null);
                    AppMethodBeat.o(88595);
                } else {
                    AppMethodBeat.o(88595);
                }
            }

            public final void onScroll(AbsListView absListView, int i, int i2, int i3) {
            }
        });
        this.kmR.a(new 5(this));
        addTextOptionMenu(0, getString(2131297812), new 6(this));
        this.Ek = findViewById(2131821926);
        bbB();
        this.kmS = v.hu(this).inflate(2130968955, null);
        this.kmT = v.hu(this).inflate(2130970112, null);
        int baS = am.baY().baS();
        if (baS > 0) {
            this.mListView.addFooterView(this.kmS);
            AppMethodBeat.o(88613);
        } else if (this.kmR.bdn() || baS != 0 || am.baY().getCount() <= 0) {
            if (this.kmR.bdn() && baS == 0) {
                am.baY().getCount();
            }
            AppMethodBeat.o(88613);
        } else {
            this.mListView.addFooterView(this.kmT);
            this.kmU = true;
            AppMethodBeat.o(88613);
        }
    }

    private void bbB() {
        AppMethodBeat.i(88614);
        if (am.bba().getCount() > 0) {
            this.mListView.setVisibility(0);
            this.Ek.setVisibility(8);
            enableOptionMenu(true);
        } else {
            this.mListView.setVisibility(8);
            this.Ek.setVisibility(0);
            enableOptionMenu(false);
        }
        this.kmR.notifyDataSetChanged();
        AppMethodBeat.o(88614);
    }

    private void au(String str, boolean z) {
        AppMethodBeat.i(88615);
        Intent intent = new Intent(this, CardDetailUI.class);
        intent.putExtra("key_card_id", str);
        intent.putExtra("key_is_share_card", z);
        intent.putExtra("key_from_scene", 4);
        startActivity(intent);
        AppMethodBeat.o(88615);
    }

    public final void a(g gVar) {
        AppMethodBeat.i(88616);
        if ((gVar.field_msg_type & 1) == 0 || (gVar.field_msg_type & 3) == 0) {
            AppMethodBeat.o(88616);
            return;
        }
        bbB();
        this.kmR.a(null, null);
        AppMethodBeat.o(88616);
    }

    public final void aVU() {
    }

    static /* synthetic */ void a(CardNewMsgUI cardNewMsgUI, int i) {
        AppMethodBeat.i(88617);
        g gVar = (g) cardNewMsgUI.kmR.getItem(i);
        if (gVar != null) {
            int i2;
            if (gVar.field_jump_type == 3) {
                if (!TextUtils.isEmpty(gVar.field_card_id)) {
                    cardNewMsgUI.au(gVar.field_card_id, false);
                } else if (TextUtils.isEmpty(gVar.field_card_tp_id)) {
                    ab.e("MicroMsg.CardNewMsgUI", "field_card_id and field_card_tp_id is null");
                } else {
                    cardNewMsgUI.au(gVar.field_card_tp_id, false);
                }
            } else if (gVar.field_jump_type == 2) {
                if (TextUtils.isEmpty(gVar.field_url)) {
                    ab.e("MicroMsg.CardNewMsgUI", "field_url is null");
                } else {
                    com.tencent.mm.plugin.card.d.b.a(cardNewMsgUI, gVar.field_url, 4);
                }
            } else if (gVar.field_jump_type == 1) {
                ab.i("MicroMsg.CardNewMsgUI", "field_jump_type is MM_CARD_ITEM_XML_MSG_JUMP_TYPE_NO");
            } else if (gVar.field_jump_type == 4) {
                if (!TextUtils.isEmpty(gVar.field_card_id)) {
                    cardNewMsgUI.au(gVar.field_card_id, true);
                } else if (TextUtils.isEmpty(gVar.field_card_tp_id)) {
                    ab.e("MicroMsg.CardNewMsgUI", "field_card_id and field_card_tp_id is null");
                } else {
                    cardNewMsgUI.au(gVar.field_card_tp_id, true);
                }
            }
            if (cardNewMsgUI.getIntent().getBooleanExtra("from_menu", false)) {
                i2 = 7;
            } else {
                i2 = 1;
            }
            com.tencent.mm.plugin.report.service.h.pYm.e(11941, new Object[]{Integer.valueOf(i2), gVar.field_card_id, gVar.field_card_tp_id, gVar.field_msg_id, BuildConfig.FLAVOR});
        }
        AppMethodBeat.o(88617);
    }

    static /* synthetic */ void b(CardNewMsgUI cardNewMsgUI, int i) {
        AppMethodBeat.i(88618);
        com.tencent.mm.ui.base.h.a(cardNewMsgUI, cardNewMsgUI.getResources().getString(2131297931), null, cardNewMsgUI.getResources().getString(2131296881), new 7(cardNewMsgUI, i));
        AppMethodBeat.o(88618);
    }

    static /* synthetic */ void h(CardNewMsgUI cardNewMsgUI) {
        AppMethodBeat.i(88620);
        d.a(cardNewMsgUI, new 8(cardNewMsgUI));
        AppMethodBeat.o(88620);
    }
}
