/*
 * Decompiled with CFR 0.152.
 */
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Vector;

public final class dx
implements Runnable {
    private static volatile Vector h;
    private static volatile boolean l;
    private static volatile boolean m;
    private static final Object i;
    private static final Object j;
    private static final byte[] o;
    private final boolean t;
    public final ke e;
    private static gd k;
    private static Boolean n;
    private static le u;
    private static le v;
    private static le w;
    private byte[] p;
    private le q;
    private le r;
    private long s;
    public boolean a;
    public long b;
    public long c;
    public int d;
    public int g;

    public static dx a(DataInput dataInput) {
        ke ke2 = ke.a(dataInput);
        byte[] byArray = new byte[dataInput.readUnsignedShort()];
        dataInput.readFully(byArray);
        byte[] byArray2 = byArray;
        return new dx(ke2, byArray2);
    }

    public static void a(gd gd2) {
        k = gd2;
    }

    static {
        i = new Object();
        j = new Object();
        n = new Boolean(false);
        o = new byte[]{67, 74, 80, 71};
    }

    private static void n() {
        Object object = j;
        synchronized (object) {
            j.notifyAll();
            return;
        }
    }

    private static void m() {
        if (!m) {
            l = false;
            h = new Vector();
            Thread thread = new Thread(new dx(null));
            thread.setPriority(Math.min(2, 5));
            thread.start();
            m = true;
        }
    }

    public final boolean equals(Object object) {
        block4: {
            block5: {
                if (this == object) break block4;
                if (!(object instanceof dx)) break block5;
                dx dx2 = (dx)object;
                if (this.e != null) {
                    return this.e.equals(dx2.e);
                }
                if (dx2.e == null) break block4;
            }
            return false;
        }
        return true;
    }

    public final int hashCode() {
        if (this.e != null) {
            return this.e.e;
        }
        return 0;
    }

    public final boolean d() {
        return this.q != null;
    }

    public final boolean c() {
        return this.p != null;
    }

    public final int e() {
        if (this.p != null) {
            return this.p.length;
        }
        return 0;
    }

    public final void a(DataOutput dataOutput) {
        this.e.a(dataOutput);
        dataOutput.writeShort(this.p.length);
        dataOutput.write(this.p);
    }

    private dx(ke ke2) {
        this(ke2, null, false);
    }

    public dx(ke ke2, le le2) {
        this(ke2, le2, false);
    }

    public static void h() {
        l = true;
        dx.n();
    }

    public final void f() {
        this.q = this.r;
        this.g = 0;
    }

    public final synchronized void b() {
        if (this.c()) {
            this.q = null;
            this.r = null;
            this.g = 0;
        }
    }

    public final void a() {
        if (this.a) {
            this.a = false;
            this.q = null;
        }
    }

    public final void a(long l, long l2) {
        if (this.d == 0 && this.s != l2 && l - this.s > (long)2000) {
            this.c = 0L;
        }
        if (this.c == 0L) {
            this.c = l;
        }
        this.s = l;
        if (this.d() && !this.a && this.d < Integer.MAX_VALUE) {
            ++this.d;
        }
    }

    public final void a(le le2, int n) {
        if (n == 0) {
            this.r = le2;
        }
        this.q = le2;
        this.g = n;
    }

    private dx(ke ke2, byte[] byArray) {
        this.e = ke2;
        this.a(byArray);
    }

    public dx(ke ke2, le le2, boolean bl2) {
        this.e = ke2;
        this.t = bl2;
        this.q = le2;
        bl2 = le2 != null;
        this.a = bl2;
    }

    private static le l() {
        gu gu2 = lh.k().g();
        if (w == null) {
            try {
                w = gu2.a("/no_tile_128.png");
            }
            catch (IOException iOException) {
                w = gu2.a(128, 128);
                me.a("MAP", iOException);
            }
        }
        return w;
    }

    private le j() {
        if (v == null || u == null) {
            gu gu2 = lh.k().g();
            try {
                u = gu2.a("/loading_tile.png");
                v = gu2.a("/blank_tile.png");
            }
            catch (IOException iOException) {
                u = v = gu2.a(128, 128);
                me.a("MAP", iOException);
            }
        }
        if (this.t) {
            return v;
        }
        return u;
    }

    public final String toString() {
        StringBuffer stringBuffer = new StringBuffer().append(this.e.toString());
        String string = this.p != null ? "B" + this.p.length : "B?";
        return stringBuffer.append(string).toString();
    }

    public final synchronized void a(byte[] byArray) {
        if (!this.c()) {
            this.g = 0;
            this.p = byArray;
            this.r = null;
            if (!this.a) {
                this.q = null;
            }
            if (m) {
                dx.a(this);
            }
            return;
        }
        throw new RuntimeException("Tile already complete");
    }

    /*
     * Enabled aggressive block sorting
     */
    public final synchronized le g() {
        block7: {
            this.b = System.currentTimeMillis();
            if ((this.q == null || this.a) && this.c()) {
                if (!m) {
                    this.i();
                    break block7;
                } else {
                    dx.a(this);
                    if (this.q == null) {
                        return this.j();
                    }
                    return this.q;
                }
            }
            if (this.q == null) {
                return this.j();
            }
        }
        if (this.q == null) {
            throw new IllegalStateException("null image");
        }
        if (this.q.b() == 128 && this.q.a() == 128) {
            return this.q;
        }
        throw new IllegalArgumentException("wrong image size: " + this.q.a() + " " + this.q.b());
    }

    private static void a(dx dx2) {
        if (dx2.c()) {
            Object object = i;
            synchronized (object) {
                if (!h.contains(dx2)) {
                    h.addElement(dx2);
                    dx.n();
                }
                return;
            }
        }
        throw new IllegalStateException("MapTile is not complete");
    }

    /*
     * WARNING - Removed back jump from a try to a catch block - possible behaviour change.
     * Unable to fully structure code
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public final void run() {
        Thread.currentThread().setPriority(1);
        while (true) {
            if (!dx.l) ** GOTO lbl-1000
            dx.k = null;
            dx.m = false;
            dx.h = null;
            return;
lbl-1000:
            // 1 sources

            {
                block19: {
                    var1_1 = dx.j;
                    synchronized (var1_1) {
                        block18: {
                            if (dx.h.size() != 0) break block18;
                            try {
                                dx.j.wait();
                                Thread.sleep(60);
                            }
                            catch (InterruptedException v0) {}
                            finally {
                                // MONITOREXIT @DISABLED, blocks:[0, 2, 5, 12, 13] lbl17 : MonitorExitStatement: MONITOREXIT : var1_1
                                break;
                            }
                        }
                        if (dx.l) continue;
                        var1_1 = dx.i;
                    }
                    synchronized (var1_1) {
                        var3_3 = null;
                        if (!dx.l && dx.h.size() > 0) {
                            var3_3 = dx.h;
                            dx.h = new Vector<E>();
                        }
                    }
                    var5_6 = System.currentTimeMillis() + (long)500;
                    break block19;
lbl-1000:
                    // 1 sources

                    {
                        while (true) {
                            var5_6 = System.currentTimeMillis() + (long)500;
                            break;
                        }
                    }
                }
                for (var2_2 = 0; var2_2 < var3_3.size() && !dx.l; ++var2_2) {
                    ((dx)var3_3.elementAt(var2_2)).i();
                    if (System.currentTimeMillis() <= var5_6) continue;
                    {
                        Thread.sleep(60);
                        ** continue;
                    }
                }
                if (dx.k == null) continue;
                dx.k.w();
                continue;
            }
            break;
        }
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    private synchronized void i() {
        block16: {
            le le2;
            long l;
            block15: {
                block17: {
                    block18: {
                        int n;
                        byte[] byArray;
                        block20: {
                            block19: {
                                block14: {
                                    if (this.q != null && !this.a) break block16;
                                    l = System.currentTimeMillis();
                                    if (dx.n != null) {
                                        if (dx.n.booleanValue() && !m) {
                                            dx.m();
                                        }
                                    } else {
                                        System.gc();
                                        l = System.currentTimeMillis();
                                    }
                                    if (this.p.length == 0) break block17;
                                    if (this.p.length == 3) break block18;
                                    byArray = this.p;
                                    byte[] byArray2 = o;
                                    if (byArray.length < 0 + byArray2.length) break block19;
                                    for (n = 0; n < byArray2.length; ++n) {
                                        if (byArray[0 + n] == byArray2[n]) {
                                            continue;
                                        }
                                        break block14;
                                    }
                                    n = 1;
                                    break block20;
                                }
                                n = 0;
                                break block20;
                            }
                            n = 0;
                        }
                        if (n != 0) {
                            byArray = this.p;
                            try {
                                byArray = gu.a(byArray);
                                le2 = lh.k().g().a(byArray, 0, byArray.length);
                            }
                            catch (IllegalArgumentException illegalArgumentException) {
                                me.a("MAP", illegalArgumentException);
                                le2 = dx.l();
                            }
                            break block15;
                        } else {
                            le2 = lh.k().g().a(this.p, 0, this.p.length);
                        }
                        break block15;
                    }
                    byte[] byArray = this.p;
                    if (byArray.length >= 3) {
                        int n = (byArray[0] & 0xFF) << 16 | (byArray[1] & 0xFF) << 8 | byArray[2] & 0xFF;
                        le2 = lh.k().g().a(128, 128);
                        ht ht2 = le2.c();
                        ht2.a(n);
                        ht2.c(0, 0, 128, 128);
                        break block15;
                    } else {
                        le2 = dx.l();
                    }
                    break block15;
                }
                le2 = dx.l();
            }
            this.a(le2, 0);
            if (n == null) {
                if (System.currentTimeMillis() - l > (long)70) {
                    n = new Boolean(true);
                    dx.m();
                } else {
                    n = new Boolean(false);
                }
            }
            this.a = false;
            System.currentTimeMillis();
        }
    }
}

