package hg;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import androidx.core.graphics.drawable.IconCompat;
import androidx.slice.Slice;
import ca.canadapost.core.data.model.RepositoryError;
import ca.canadapost.core.data.model.ResultWrapper;
import ca.canadapost.module.track.TrackActivity;
import ca.canadapost.module.track.api.ITrackApi;
import ca.canadapost.module.track.model.DeliveryDate;
import ca.canadapost.module.track.model.DeliveryDateLabel;
import ca.canadapost.module.track.model.Event;
import ca.canadapost.module.track.model.PackageDTO;
import ca.canadapost.module.track.model.PackageDTOKt;
import ca.canadapost.module.track.model.PackageDetailsDTO;
import ca.canadapost.module.track.model.PackageEventType;
import ca.canadapost.module.track.model.PackageItemModel;
import dn.p;
import java.util.Date;
import k5.a;
import kotlin.coroutines.jvm.internal.l;
import kotlin.jvm.internal.t;
import rm.n0;
import rm.y;
import tp.a1;
import tp.k;
import tp.m0;
import y7.f;
import yf.h;

/* loaded from: classes4.dex */
public class b implements hg.a {

    /* renamed from: a, reason: collision with root package name */
    private final Context f21649a;

    /* renamed from: b, reason: collision with root package name */
    private final Uri f21650b;

    /* renamed from: c, reason: collision with root package name */
    public ITrackApi f21651c;

    /* renamed from: d, reason: collision with root package name */
    public ca.a f21652d;

    /* renamed from: e, reason: collision with root package name */
    private final m0 f21653e;

    /* renamed from: f, reason: collision with root package name */
    private final String f21654f;

    /* renamed from: g, reason: collision with root package name */
    private ResultWrapper f21655g;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public static final class a extends kotlin.coroutines.jvm.internal.d {

        /* renamed from: p, reason: collision with root package name */
        Object f21656p;

        /* renamed from: q, reason: collision with root package name */
        Object f21657q;

        /* renamed from: r, reason: collision with root package name */
        /* synthetic */ Object f21658r;

        /* renamed from: t, reason: collision with root package name */
        int f21660t;

        a(vm.d dVar) {
            super(dVar);
        }

        @Override // kotlin.coroutines.jvm.internal.a
        public final Object invokeSuspend(Object obj) {
            this.f21658r = obj;
            this.f21660t |= Integer.MIN_VALUE;
            return b.this.j(null, this);
        }
    }

    /* renamed from: hg.b$b, reason: collision with other inner class name */
    /* loaded from: classes4.dex */
    static final class C0462b extends l implements p {

        /* renamed from: p, reason: collision with root package name */
        Object f21661p;

        /* renamed from: q, reason: collision with root package name */
        int f21662q;

        C0462b(vm.d dVar) {
            super(2, dVar);
        }

        @Override // kotlin.coroutines.jvm.internal.a
        public final vm.d create(Object obj, vm.d dVar) {
            return new C0462b(dVar);
        }

        @Override // kotlin.coroutines.jvm.internal.a
        public final Object invokeSuspend(Object obj) {
            Object f10;
            b bVar;
            f10 = wm.d.f();
            int i10 = this.f21662q;
            if (i10 == 0) {
                y.b(obj);
                b bVar2 = b.this;
                String str = bVar2.f21654f;
                this.f21661p = bVar2;
                this.f21662q = 1;
                Object j10 = bVar2.j(str, this);
                if (j10 == f10) {
                    return f10;
                }
                bVar = bVar2;
                obj = j10;
            } else {
                if (i10 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                bVar = (b) this.f21661p;
                y.b(obj);
            }
            bVar.f21655g = (ResultWrapper) obj;
            b.this.l().getContentResolver().notifyChange(b.this.o(), null);
            return n0.f34904a;
        }

        @Override // dn.p
        public final Object invoke(m0 m0Var, vm.d dVar) {
            return ((C0462b) create(m0Var, dVar)).invokeSuspend(n0.f34904a);
        }
    }

    public b(Context context, Uri sliceUri) {
        t.g(context, "context");
        t.g(sliceUri, "sliceUri");
        this.f21649a = context;
        this.f21650b = sliceUri;
        this.f21653e = tp.n0.a(a1.b());
        String queryParameter = sliceUri.getQueryParameter("trackingName");
        this.f21654f = new kotlin.text.l("\\s+").g(queryParameter == null ? "" : queryParameter, "");
        Context applicationContext = context.getApplicationContext();
        t.f(applicationContext, "getApplicationContext(...)");
        f.a(this, applicationContext);
    }

    private final k5.b e() {
        k5.b b10 = k5.b.b(PendingIntent.getActivity(this.f21649a.getApplicationContext(), 0, new Intent(this.f21649a.getApplicationContext(), (Class<?>) TrackActivity.class), 67108864), IconCompat.c(this.f21649a.getApplicationContext(), yf.c.f52412u), 1, "BASIC ACTION");
        t.f(b10, "create(...)");
        return b10;
    }

    private final k5.b f(PackageItemModel packageItemModel) {
        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("app://www.canadapost.ca/?trackingNumber=" + packageItemModel.getItemNumber()));
        intent.putExtra("INTENT_EXTRA_TRACKITEM_ID", packageItemModel.getItemNumber());
        k5.b b10 = k5.b.b(PendingIntent.getActivity(this.f21649a.getApplicationContext(), 0, intent, 67108864), IconCompat.c(this.f21649a.getApplicationContext(), yf.c.f52412u), 1, "TRACKING_ACTION");
        t.f(b10, "create(...)");
        return b10;
    }

    private final Slice g(String str) {
        k5.a aVar = new k5.a(this.f21649a.getApplicationContext(), this.f21650b, -1L);
        a.b r10 = new a.b(this.f21650b).u("Loading " + str + " status...").r(e());
        t.f(r10, "setPrimaryAction(...)");
        aVar.g(r10);
        Slice h10 = aVar.h();
        t.f(h10, "build(...)");
        return h10;
    }

    private final Slice h() {
        k5.a aVar = new k5.a(this.f21649a.getApplicationContext(), this.f21650b, -1L);
        a.b r10 = new a.b(this.f21650b).u("Package " + this.f21654f + " not found!").r(e());
        t.f(r10, "setPrimaryAction(...)");
        aVar.g(r10);
        Slice h10 = aVar.h();
        t.f(h10, "build(...)");
        return h10;
    }

    private final Slice i(PackageItemModel packageItemModel) {
        String str;
        k5.a i10 = new k5.a(this.f21649a.getApplicationContext(), this.f21650b, -1L).i(p(packageItemModel));
        t.f(i10, "setAccentColor(...)");
        a.C0530a m10 = new a.C0530a(this.f21650b).m(this.f21649a.getString(h.O2) + " " + packageItemModel.getItemNumber());
        Event event = packageItemModel.getLastEvent().getEvent();
        if (event == null || (str = event.getDescEn()) == null) {
            str = "Not found";
        }
        a.C0530a k10 = m10.k(str);
        t.f(k10, "setSubtitle(...)");
        a.b w10 = new a.b(this.f21650b).w(IconCompat.c(this.f21649a.getApplicationContext(), ((Number) packageItemModel.getStatusIcon().getResId().d()).intValue()), 0);
        String userDescription = packageItemModel.getUserDescription();
        if (userDescription == null) {
            userDescription = packageItemModel.getItemNumber();
        }
        a.b r10 = w10.u(userDescription).s(m(packageItemModel)).r(f(packageItemModel));
        t.f(r10, "setPrimaryAction(...)");
        i10.j(k10);
        i10.g(r10);
        Slice h10 = i10.h();
        t.f(h10, "build(...)");
        return h10;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Removed duplicated region for block: B:32:0x007b  */
    /* JADX WARN: Removed duplicated region for block: B:36:0x0041  */
    /* JADX WARN: Removed duplicated region for block: B:8:0x0024  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object j(String str, vm.d dVar) {
        a aVar;
        Object f10;
        int i10;
        ResultWrapper resultWrapper;
        ResultWrapper resultWrapper2;
        if (dVar instanceof a) {
            aVar = (a) dVar;
            int i11 = aVar.f21660t;
            if ((i11 & Integer.MIN_VALUE) != 0) {
                aVar.f21660t = i11 - Integer.MIN_VALUE;
                Object obj = aVar.f21658r;
                f10 = wm.d.f();
                i10 = aVar.f21660t;
                if (i10 != 0) {
                    y.b(obj);
                    if (PackageDTOKt.isValidPin(str)) {
                        ITrackApi k10 = k();
                        aVar.f21656p = this;
                        aVar.f21657q = str;
                        aVar.f21660t = 1;
                        obj = k10.trackPinDetails(str, aVar);
                        if (obj == f10) {
                            return f10;
                        }
                    }
                    if (PackageDTOKt.isValidDnc(str)) {
                        ITrackApi k11 = this.k();
                        aVar.f21656p = null;
                        aVar.f21657q = null;
                        aVar.f21660t = 2;
                        obj = k11.trackDncDetails(str, aVar);
                        if (obj == f10) {
                            return f10;
                        }
                        resultWrapper2 = (ResultWrapper) obj;
                        if (!(resultWrapper2 instanceof ResultWrapper.Error)) {
                        }
                        return resultWrapper2;
                    }
                    return new ResultWrapper.Error(null, null, RepositoryError.Unknown, null, 8, null);
                }
                if (i10 != 1) {
                    if (i10 != 2) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    y.b(obj);
                    resultWrapper2 = (ResultWrapper) obj;
                    if (!(resultWrapper2 instanceof ResultWrapper.Error) || ((resultWrapper2 instanceof ResultWrapper.Success) && ((PackageDTO) ((ResultWrapper.Success) resultWrapper2).getData()).getError() == null)) {
                        return resultWrapper2;
                    }
                    return new ResultWrapper.Error(null, null, RepositoryError.Unknown, null, 8, null);
                }
                str = (String) aVar.f21657q;
                this = (b) aVar.f21656p;
                y.b(obj);
                resultWrapper = (ResultWrapper) obj;
                if (!(resultWrapper instanceof ResultWrapper.Error) || ((resultWrapper instanceof ResultWrapper.Success) && ((PackageDTO) ((ResultWrapper.Success) resultWrapper).getData()).getError() == null)) {
                    return resultWrapper;
                }
                if (PackageDTOKt.isValidDnc(str)) {
                }
                return new ResultWrapper.Error(null, null, RepositoryError.Unknown, null, 8, null);
            }
        }
        aVar = new a(dVar);
        Object obj2 = aVar.f21658r;
        f10 = wm.d.f();
        i10 = aVar.f21660t;
        if (i10 != 0) {
        }
        resultWrapper = (ResultWrapper) obj2;
        if (!(resultWrapper instanceof ResultWrapper.Error)) {
        }
        return resultWrapper;
    }

    private final String m(PackageItemModel packageItemModel) {
        if (packageItemModel.getDeliveryDate() == null) {
            return "";
        }
        DeliveryDate deliveryDate = packageItemModel.getDeliveryDate();
        Date date = deliveryDate.getDate();
        if (date != null && deliveryDate.getLabel() == DeliveryDateLabel.PICKUP_BY && w8.h.f40968a.h(date.getTime())) {
            return "";
        }
        String string = deliveryDate.getDate() == null ? this.f21649a.getString(h.f52659w) : (deliveryDate.getLabel() == DeliveryDateLabel.DELIVERED || !w8.h.f40968a.h(deliveryDate.getDate().getTime())) ? ig.a.f23217a.a(deliveryDate.getDate().getTime(), this.f21649a, n()) : this.f21649a.getString(h.Z);
        t.d(string);
        return this.f21649a.getString(deliveryDate.getLabel().getResId()) + ": " + string;
    }

    private final int p(PackageItemModel packageItemModel) {
        Context context = this.f21649a;
        PackageEventType lastEvent = packageItemModel.getLastEvent();
        return context.getColor(lastEvent instanceof PackageEventType.DELIVERED ? yf.b.f52387i : lastEvent instanceof PackageEventType.ATTENTION ? yf.b.f52390l : lastEvent instanceof PackageEventType.IN_TRANSIT ? yf.b.f52386h : lastEvent instanceof PackageEventType.OUT_FOR_DELIVERY ? yf.b.f52386h : lastEvent instanceof PackageEventType.AVAILABLE_FOR_PICKUP ? yf.b.f52386h : lastEvent instanceof PackageEventType.RETURNED_ITEM ? yf.b.f52386h : lastEvent instanceof PackageEventType.RETURN_RTS ? yf.b.f52389k : yf.b.f52390l);
    }

    @Override // hg.a
    public Slice a() {
        ResultWrapper resultWrapper = this.f21655g;
        if (resultWrapper != null) {
            if (resultWrapper instanceof ResultWrapper.Success) {
                t.e(resultWrapper, "null cannot be cast to non-null type ca.canadapost.core.data.model.ResultWrapper.Success<ca.canadapost.module.track.model.PackageDTO>");
                if (((PackageDTO) ((ResultWrapper.Success) resultWrapper).getData()).getError() == null) {
                    String str = this.f21654f;
                    Date date = new Date(System.currentTimeMillis());
                    Date date2 = new Date(System.currentTimeMillis());
                    ResultWrapper resultWrapper2 = this.f21655g;
                    t.e(resultWrapper2, "null cannot be cast to non-null type ca.canadapost.core.data.model.ResultWrapper.Success<ca.canadapost.module.track.model.PackageDetailsDTO>");
                    return i(new PackageItemModel(str, null, date, date2, false, false, null, (PackageDetailsDTO) ((ResultWrapper.Success) resultWrapper2).getData(), null, null, null, null, null, null, null, false, false, false, 261984, null));
                }
            }
            ResultWrapper resultWrapper3 = this.f21655g;
            if (resultWrapper3 instanceof ResultWrapper.Success) {
                return h();
            }
            if (resultWrapper3 instanceof ResultWrapper.Error) {
                return h();
            }
        }
        k.d(this.f21653e, null, null, new C0462b(null), 3, null);
        return g(this.f21654f);
    }

    public final ITrackApi k() {
        ITrackApi iTrackApi = this.f21651c;
        if (iTrackApi != null) {
            return iTrackApi;
        }
        t.y("api");
        return null;
    }

    public final Context l() {
        return this.f21649a;
    }

    public final ca.a n() {
        ca.a aVar = this.f21652d;
        if (aVar != null) {
            return aVar;
        }
        t.y("languageService");
        return null;
    }

    public final Uri o() {
        return this.f21650b;
    }
}
