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

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.ai.f;
import com.tencent.mm.ai.m;
import com.tencent.mm.model.aw;
import com.tencent.mm.model.r;
import com.tencent.mm.plugin.label.b.d;
import com.tencent.mm.plugin.label.c;
import com.tencent.mm.plugin.label.e;
import com.tencent.mm.plugin.label.ui.widget.MMLabelPanel;
import com.tencent.mm.plugin.report.service.h;
import com.tencent.mm.protocal.protobuf.cmh;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.al;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.mm.storage.af;
import com.tencent.mm.ui.base.MMTagPanel.a;
import com.tencent.mm.ui.base.MMTagPanelScrollView;
import com.tencent.mm.ui.q.b;
import com.tencent.ytcommon.BuildConfig;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;

public class ContactLabelUI extends ContactLabelBaseUI implements f {
    private String edV;
    private MMTagPanelScrollView nIC;
    private MMLabelPanel nID;
    private TextView nIE;
    private View nIF;
    private View nIG;
    private TextView nIH;
    private MMLabelPanel nII;
    private ListView nIJ;
    private ScrollView nIK;
    private b nIL;
    private String nIM;
    private ArrayList<String> nIN;
    private HashSet<String> nIO = new HashSet();
    private HashSet<String> nIP = new HashSet();
    private ArrayList<String> nIQ = new ArrayList();
    private a nIR = a.nIW;
    private boolean nIS = false;
    private ArrayList<String> nIT;
    private boolean nId = true;

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

    public ContactLabelUI() {
        AppMethodBeat.i(22680);
        AppMethodBeat.o(22680);
    }

    static /* synthetic */ void a(ContactLabelUI contactLabelUI, String str, boolean z) {
        AppMethodBeat.i(22698);
        contactLabelUI.aR(str, z);
        AppMethodBeat.o(22698);
    }

    public final int getLayoutId() {
        return 2130969230;
    }

    public final void initView() {
        AppMethodBeat.i(22681);
        setMMTitle(getString(2131296408));
        setBackBtn(new OnMenuItemClickListener() {
            public final boolean onMenuItemClick(MenuItem menuItem) {
                AppMethodBeat.i(22658);
                ContactLabelUI.this.onBackPressed();
                AppMethodBeat.o(22658);
                return false;
            }
        });
        a(0, getString(2131297038), new 5(this), b.ymu);
        this.nIC = (MMTagPanelScrollView) findViewById(2131823070);
        this.nIC.setMaxLine(3);
        this.nID = (MMLabelPanel) findViewById(2131823071);
        this.nIE = (TextView) findViewById(2131823072);
        this.nIF = findViewById(2131823074);
        this.nIG = findViewById(2131823076);
        this.nIG.setBackgroundDrawable(null);
        this.nIH = (TextView) this.nIG.findViewById(16908310);
        this.nIH.setText(2131300894);
        this.nII = (MMLabelPanel) findViewById(2131823077);
        this.nIJ = (ListView) findViewById(2131823078);
        this.nIK = (ScrollView) findViewById(2131823073);
        if (this.nIK != null) {
            this.nIK.setOnTouchListener(new 6(this));
        }
        this.nID.yxp = true;
        this.nID.qe(true);
        this.nID.setTagEditTextBG(2130840373);
        this.nID.setCallBack(new a() {
            public final void JR(String str) {
                AppMethodBeat.i(22664);
                ab.d("MicroMsg.Label.ContactLabelUI", "cpan[Input onTagUnSelected] tag:%s", new Object[]{str});
                ContactLabelUI.this.nID.removeTag(str);
                if (ContactLabelUI.this.nII != null) {
                    ContactLabelUI.this.nII.cd(str, false);
                }
                ContactLabelUI.a(ContactLabelUI.this, str);
                AppMethodBeat.o(22664);
            }

            public final void JS(String str) {
                AppMethodBeat.i(22665);
                ab.d("MicroMsg.Label.ContactLabelUI", "cpan[Input onTagSelected] tag:%s", new Object[]{str});
                AppMethodBeat.o(22665);
            }

            public final void JT(String str) {
                AppMethodBeat.i(22666);
                ab.d("MicroMsg.Label.ContactLabelUI", "cpan[Input onTagRemove] tag:%s", new Object[]{str});
                if (ContactLabelUI.this.nII != null) {
                    ContactLabelUI.this.nII.cd(str, false);
                }
                ContactLabelUI.a(ContactLabelUI.this, str);
                AppMethodBeat.o(22666);
            }

            public final void bmm() {
                AppMethodBeat.i(22667);
                ab.d("MicroMsg.Label.ContactLabelUI", "cpan[Input onTagEditTextClick]");
                AppMethodBeat.o(22667);
            }

            public final void JU(String str) {
                AppMethodBeat.i(22668);
                ab.d("MicroMsg.Label.ContactLabelUI", "cpan[Input onTagEditTextChange] curText:%s", new Object[]{str});
                ContactLabelUI.b(ContactLabelUI.this, str);
                AppMethodBeat.o(22668);
            }

            public final void JV(String str) {
                AppMethodBeat.i(22669);
                ab.d("MicroMsg.Label.ContactLabelUI", "cpan[Input onTagCreate] tag:%s", new Object[]{String.valueOf(str)});
                if (bo.isNullOrNil(str)) {
                    ab.d("MicroMsg.Label.ContactLabelUI", "tag is null.");
                    AppMethodBeat.o(22669);
                    return;
                }
                ContactLabelUI.this.nID.cc(str, true);
                if (ContactLabelUI.this.nII != null) {
                    ContactLabelUI.this.nII.cd(str, true);
                }
                ContactLabelUI.a(ContactLabelUI.this, str, ContactLabelUI.this.nIS);
                AppMethodBeat.o(22669);
            }

            public final void q(boolean z, int i) {
                AppMethodBeat.i(22670);
                ab.d("MicroMsg.Label.ContactLabelUI", "cpan[Input onTagLengthMax] match:%s exceedCount:%d", new Object[]{String.valueOf(z), Integer.valueOf(i)});
                if (z) {
                    ContactLabelUI.this.enableOptionMenu(false);
                    ContactLabelUI.this.nIE.setVisibility(0);
                    ContactLabelUI.this.nIE.setText(2131300895);
                    ContactLabelUI.this.nIE.setText(String.format(ContactLabelUI.this.getString(2131300895), new Object[]{Integer.valueOf(com.tencent.mm.ui.tools.f.bP(36, BuildConfig.FLAVOR)), Integer.valueOf(i)}));
                    AppMethodBeat.o(22670);
                    return;
                }
                ContactLabelUI.this.enableOptionMenu(true);
                ContactLabelUI.this.nIE.setVisibility(8);
                AppMethodBeat.o(22670);
            }
        });
        this.nII.qe(false);
        this.nII.setCallBack(new a() {
            public final void JR(String str) {
                AppMethodBeat.i(22671);
                ab.d("MicroMsg.Label.ContactLabelUI", "cpan[All onTagUnSelected] tag:%s", new Object[]{String.valueOf(str)});
                if (ContactLabelUI.this.nID != null) {
                    ContactLabelUI.this.nID.removeTag(str);
                }
                ContactLabelUI.a(ContactLabelUI.this, str);
                AppMethodBeat.o(22671);
            }

            public final void JS(String str) {
                AppMethodBeat.i(22672);
                ab.d("MicroMsg.Label.ContactLabelUI", "cpan[All onTagSelected] tag:%s", new Object[]{String.valueOf(str)});
                if (ContactLabelUI.this.nID != null) {
                    ContactLabelUI.this.nID.cc(str, true);
                }
                ContactLabelUI.a(ContactLabelUI.this, str, ContactLabelUI.this.nIS);
                AppMethodBeat.o(22672);
            }

            public final void JT(String str) {
            }

            public final void q(boolean z, int i) {
            }

            public final void bmm() {
            }

            public final void JU(String str) {
            }

            public final void JV(String str) {
            }
        });
        this.nIJ.setAdapter(this.nIL);
        this.nIJ.setOnItemClickListener(new 9(this));
        enableOptionMenu(false);
        AppMethodBeat.o(22681);
    }

    public void onCreate(Bundle bundle) {
        AppMethodBeat.i(22682);
        super.onCreate(bundle);
        this.nIM = getIntent().getStringExtra("label_id_list");
        this.nIN = getIntent().getStringArrayListExtra("label_str_list");
        this.edV = getIntent().getStringExtra("label_username");
        this.nIS = getIntent().getBooleanExtra("is_stranger", false);
        this.nIL = new b(this);
        initView();
        if (!(bo.isNullOrNil(this.nIM) || this.nIN == null || this.nIN.size() <= 0)) {
            this.nID.a(this.nIN, this.nIN);
        }
        if (this.nIS) {
            this.nIT = getIntent().getStringArrayListExtra("label_str_list");
            this.nID.a(this.nIT, this.nIT);
        }
        h.pYm.e(16097, new Object[]{Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(1)});
        AppMethodBeat.o(22682);
    }

    public void onResume() {
        AppMethodBeat.i(22683);
        aw.Rg().a(635, this);
        aw.Rg().a(638, this);
        al.d(new 12(this));
        super.onResume();
        AppMethodBeat.o(22683);
    }

    public void onPause() {
        AppMethodBeat.i(22684);
        aw.Rg().b(635, this);
        aw.Rg().b(638, this);
        super.onPause();
        AppMethodBeat.o(22684);
    }

    public void onDestroy() {
        AppMethodBeat.i(22685);
        super.onDestroy();
        AppMethodBeat.o(22685);
    }

    public void onSceneEnd(int i, int i2, String str, m mVar) {
        AppMethodBeat.i(22686);
        ab.i("MicroMsg.Label.ContactLabelUI", "cpan[onSceneEnd]errType:%d errCode:%d errMsg:%s", new Object[]{Integer.valueOf(i), Integer.valueOf(i2), str});
        switch (mVar.getType()) {
            case 635:
                if (i == 0 && i2 == 0) {
                    bJl();
                    AppMethodBeat.o(22686);
                    return;
                }
                bJf();
                AppMethodBeat.o(22686);
                return;
            case 638:
                if (i == 0 && i2 == 0) {
                    ab.i("MicroMsg.Label.ContactLabelUI", "cpan[onSceneEnd] success.");
                    bJm();
                    AppMethodBeat.o(22686);
                    return;
                }
                bJf();
                AppMethodBeat.o(22686);
                return;
            default:
                ab.w("MicroMsg.Label.ContactLabelUI", "unknow type.");
                AppMethodBeat.o(22686);
                return;
        }
    }

    public void onBackPressed() {
        AppMethodBeat.i(22687);
        if (bJn()) {
            com.tencent.mm.ui.base.h.d(this, getString(2131302710), BuildConfig.FLAVOR, getString(2131297761), getString(2131297762), new 2(this), new 3(this));
            AppMethodBeat.o(22687);
            return;
        }
        super.onBackPressed();
        AppMethodBeat.o(22687);
    }

    private void a(a aVar) {
        AppMethodBeat.i(22688);
        this.nIR = aVar;
        switch (4.nIV[this.nIR.ordinal()]) {
            case 1:
                this.nIJ.setVisibility(8);
                if (this.nIQ == null || this.nIQ.size() <= 0) {
                    this.nIK.setVisibility(8);
                    this.nIF.setVisibility(8);
                } else {
                    this.nIF.setVisibility(0);
                    this.nIK.setVisibility(0);
                }
                this.nIE.setVisibility(8);
                AppMethodBeat.o(22688);
                return;
            case 2:
                this.nIK.setVisibility(8);
                this.nIJ.setVisibility(0);
                this.nIF.setVisibility(8);
                this.nIE.setVisibility(8);
                AppMethodBeat.o(22688);
                return;
            case 3:
                this.nIJ.setVisibility(8);
                this.nIK.setVisibility(8);
                this.nIF.setVisibility(8);
                this.nIE.setVisibility(0);
                this.nIE.setText(2131300895);
                AppMethodBeat.o(22688);
                return;
            case 4:
                this.nIJ.setVisibility(8);
                this.nIK.setVisibility(8);
                this.nIF.setVisibility(8);
                this.nIE.setVisibility(0);
                this.nIE.setText(2131300895);
                break;
        }
        AppMethodBeat.o(22688);
    }

    private void aR(String str, boolean z) {
        AppMethodBeat.i(22689);
        if (bo.isNullOrNil(str)) {
            ab.w("MicroMsg.Label.ContactLabelUI", "tag is empty");
            AppMethodBeat.o(22689);
            return;
        }
        String trim = str.trim();
        if (bo.isNullOrNil(trim)) {
            ab.w("MicroMsg.Label.ContactLabelUI", "new tag is empty");
            AppMethodBeat.o(22689);
            return;
        }
        af aoE = e.bIZ().aoE(trim);
        if (z) {
            if (this.nIQ == null || !this.nIQ.contains(trim) || aoE == null) {
                this.nIP.add(trim);
            }
        } else if (this.nIQ == null || !this.nIQ.contains(trim) || (aoE != null && aoE.field_isTemporary)) {
            this.nIP.add(trim);
        }
        if (this.nIO != null && this.nIO.contains(trim)) {
            this.nIO.remove(trim);
        }
        if (bJn()) {
            enableOptionMenu(true);
        }
        AppMethodBeat.o(22689);
    }

    private void bJl() {
        AppMethodBeat.i(22690);
        ab.i("MicroMsg.Label.ContactLabelUI", "cpan[saveContact]");
        if (this.nID != null) {
            int size;
            int size2;
            int size3;
            ab.i("MicroMsg.Label.ContactLabelUI", "cpan[saveContact] doScene");
            String str = BuildConfig.FLAVOR;
            ArrayList tagList = this.nID.getTagList();
            if (tagList != null && tagList.size() > 0) {
                str = c.cf(e.bIZ().ai(tagList));
            }
            LinkedList linkedList = new LinkedList();
            cmh cmh = new cmh();
            cmh.wcD = str;
            cmh.jBB = this.edV;
            linkedList.add(cmh);
            aw.Rg().a(new d(linkedList), 0);
            if (this.nIP != null) {
                size = this.nIP.size();
            } else {
                size = 0;
            }
            if (this.nIN != null) {
                size2 = this.nIN.size();
            } else {
                size2 = 0;
            }
            if (this.nIO != null) {
                size3 = this.nIO.size();
            } else {
                size3 = 0;
            }
            size2 = ((size3 + this.nID.getTagList().size()) - size2) - size;
            if (size > 0 || size2 > 0) {
                ab.d("MicroMsg.Label.ContactLabelUI", "cpan[saveContact]addLabelNum:%d,updateLabelNum:%d", new Object[]{Integer.valueOf(size), Integer.valueOf(size2)});
                h.pYm.e(11220, new Object[]{r.Yz(), Integer.valueOf(size), Integer.valueOf(size2), Integer.valueOf(0), Integer.valueOf(0)});
            }
            if (size > 0) {
                h.pYm.e(16097, new Object[]{Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(1)});
            }
        }
        AppMethodBeat.o(22690);
    }

    private void bJm() {
        AppMethodBeat.i(22691);
        bJc();
        this.nIP.clear();
        this.nIO.clear();
        finish();
        AppMethodBeat.o(22691);
    }

    private void bJf() {
        AppMethodBeat.i(22692);
        bJc();
        JN(getString(2131296405));
        AppMethodBeat.o(22692);
    }

    private boolean bJn() {
        AppMethodBeat.i(22693);
        if (this.nID != null) {
            if (this.nIN == null || this.nIN.size() <= 0) {
                if (this.nID.getTagList() != null && this.nID.getTagList().size() > 0) {
                    AppMethodBeat.o(22693);
                    return true;
                }
            } else if (this.nID.getTagList() != null || this.nID.getTagList().size() > 0) {
                ArrayList tagList = this.nID.getTagList();
                Collections.sort(this.nIN);
                Collections.sort(tagList);
                if (this.nIN.equals(tagList)) {
                    AppMethodBeat.o(22693);
                    return false;
                }
                AppMethodBeat.o(22693);
                return true;
            } else {
                AppMethodBeat.o(22693);
                return true;
            }
        }
        AppMethodBeat.o(22693);
        return false;
    }

    public void finish() {
        AppMethodBeat.i(22694);
        if (this.nID != null) {
            this.nID.dAd();
        }
        Intent intent = new Intent();
        if (bJn()) {
            intent.putExtra("hasLableChange", true);
        } else {
            intent.putExtra("hasLableChange", false);
        }
        setResult(-1, intent);
        super.finish();
        AppMethodBeat.o(22694);
    }
}
