package com.ss.android.ugc.aweme.watermark;

import X.C4220NtX;
import X.C4278NuT;
import X.C4282NuX;
import X.C4907OBa;
import X.C4908OBb;
import X.C4911OBe;
import X.C4912OBf;
import X.FWb;
import X.JUB;
import X.Lnj;
import X.M12;
import X.NO8;
import X.OBW;
import X.OBX;
import X.OBY;
import X.OBZ;
import X.Qx3;
import android.app.Application;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.text.TextPaint;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.ss.android.ugc.aweme.out.AVExternalServiceImpl;
import com.ss.android.ugc.aweme.services.watermark.IWaterMarkService;
import java.io.File;
import java.io.FileOutputStream;
import kotlin.h.b.n;

public final class WaterMarkServiceImpl implements IWaterMarkService {
    public static final C4908OBb LIZ = new C4908OBb((byte) 0);

    static {
        Covode.recordClassIndex(124133);
    }

    public final void cancelWaterMark() {
        AVExternalServiceImpl.LIZ().asyncServiceWithOutPanel("WaterMark", new OBW(this));
    }

    public final OBZ LIZ() {
        if (FWb.LIZIZ.LIZ().LJIIZILJ().LIZLLL()) {
            return (C4912OBf) C4907OBa.LIZIZ.getValue();
        }
        return (C4911OBe) C4907OBa.LIZ.getValue();
    }

    public static IWaterMarkService LIZIZ() {
        MethodCollector.i(3549);
        IWaterMarkService iWaterMarkService = (IWaterMarkService) Lnj.LIZ(IWaterMarkService.class, false);
        if (iWaterMarkService != null) {
            MethodCollector.o(3549);
            return iWaterMarkService;
        }
        Object LIZIZ = Lnj.LIZIZ(IWaterMarkService.class, false);
        if (LIZIZ != null) {
            IWaterMarkService iWaterMarkService2 = (IWaterMarkService) LIZIZ;
            MethodCollector.o(3549);
            return iWaterMarkService2;
        }
        if (Lnj.bX == null) {
            synchronized (IWaterMarkService.class) {
                try {
                    if (Lnj.bX == null) {
                        Lnj.bX = new WaterMarkServiceImpl();
                    }
                } catch (Throwable th) {
                    MethodCollector.o(3549);
                    throw th;
                }
            }
        }
        WaterMarkServiceImpl waterMarkServiceImpl = (WaterMarkServiceImpl) Lnj.bX;
        MethodCollector.o(3549);
        return waterMarkServiceImpl;
    }

    public final synchronized void prepareDataForI18n(JUB jub) {
        MethodCollector.i(3475);
        M12.LIZ(jub);
        AVExternalServiceImpl.LIZ().asyncServiceWithOutPanel("WaterMark", new OBY(this, jub));
        MethodCollector.o(3475);
    }

    public final synchronized void waterMark(JUB jub) {
        MethodCollector.i(3373);
        M12.LIZ(jub);
        AVExternalServiceImpl.LIZ().asyncServiceWithOutPanel("WaterMark", new OBX(this, jub));
        MethodCollector.o(3373);
    }

    public final void addWaterMarkToImage(NO8 no8, String str, String str2) {
        String LIZ2;
        boolean z;
        int i;
        float f;
        float f2;
        MethodCollector.i(3362);
        String str3 = str;
        String str4 = str2;
        NO8 no82 = no8;
        M12.LIZ(no82, str3, str4);
        M12.LIZ(str3, no82);
        if (new File(str3).exists()) {
            Bitmap decodeFile = BitmapFactory.decodeFile(str3);
            n.LIZIZ(decodeFile, "");
            int width = decodeFile.getWidth();
            int height = decodeFile.getHeight();
            float f3 = (float) width;
            float f4 = (float) height;
            float f5 = f3 / f4;
            if (f5 < 0.8f) {
                height = ((int) (((float) (height - width)) / 2.0f)) + width;
            } else if (f5 > 1.0f || f5 <= 0.8f) {
                if (f5 > 1.0f) {
                    width = ((int) (((float) (width - height)) / 2.0f)) + height;
                } else {
                    width = 0;
                    height = 0;
                }
            }
            Point point = new Point(width, height);
            float f6 = f3 / 750.0f;
            float f7 = f4 / 750.0f;
            float LIZJ = Qx3.LIZJ(f6, f7) * 10.0f;
            point.set((int) (((float) point.x) - LIZJ), (int) (((float) point.y) - LIZJ));
            if (no82 == null) {
                LIZ2 = "";
            } else if (TextUtils.isEmpty(no82.LIZ())) {
                LIZ2 = no82.LIZIZ();
            } else {
                LIZ2 = no82.LIZ();
            }
            String valueOf = String.valueOf(LIZ2);
            int i2 = point.x;
            int i3 = point.y;
            if (no82 == null || !TextUtils.equals(C4220NtX.LJIJ.LIZJ(), no82.LIZJ())) {
                z = false;
            } else {
                z = true;
            }
            Bitmap copy = decodeFile.copy(Bitmap.Config.ARGB_8888, true);
            decodeFile.recycle();
            Canvas canvas = new Canvas(copy);
            float LIZJ2 = Qx3.LIZJ(f6, f7);
            Application application = C4220NtX.LIZ;
            n.LIZIZ(application, "");
            Resources resources = application.getResources();
            n.LIZIZ(resources, "");
            TextPaint textPaint = new TextPaint();
            textPaint.setAntiAlias(true);
            textPaint.setTypeface(C4278NuT.LIZ().LIZ(C4282NuX.LIZ));
            textPaint.setColor(resources.getColor(2131099685));
            float f8 = 28.0f * LIZJ2;
            textPaint.setTextSize(f8);
            float f9 = 4.0f * LIZJ2;
            textPaint.setShadowLayer(f9, 0.0f, LIZJ2, resources.getColor(2131099916));
            int i4 = (int) f8;
            int measureText = (int) textPaint.measureText(valueOf);
            if (z) {
                i = i4;
            } else {
                i = 0;
            }
            float f10 = ((float) (i + i4 + i4 + measureText)) + f9;
            int i5 = (int) (32.0f * LIZJ2);
            float f11 = (float) i5;
            canvas.translate(((float) i2) - f10, ((float) i3) - f11);
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            Bitmap decodeResource = BitmapFactory.decodeResource(resources, 2131230980, (BitmapFactory.Options) null);
            if (decodeResource == null) {
                n.LIZIZ();
            }
            canvas.drawBitmap(Bitmap.createScaledBitmap(decodeResource, i4, i4, true), 0.0f, ((float) (i5 - i4)) / 2.0f, paint);
            if (z) {
                Bitmap decodeResource2 = BitmapFactory.decodeResource(resources, 2131230989, (BitmapFactory.Options) null);
                if (decodeResource2 == null) {
                    n.LIZIZ();
                }
                Bitmap createScaledBitmap = Bitmap.createScaledBitmap(decodeResource2, i4, i4, true);
                float f12 = (float) i4;
                canvas.drawBitmap(createScaledBitmap, f12, (f11 - f12) / 2.0f, paint);
            }
            Bitmap decodeResource3 = BitmapFactory.decodeResource(resources, 2131230954, (BitmapFactory.Options) null);
            if (decodeResource3 == null) {
                n.LIZIZ();
            }
            Bitmap createScaledBitmap2 = Bitmap.createScaledBitmap(decodeResource3, i4, i4, true);
            float f13 = (float) i4;
            if (z) {
                f2 = f13;
                f = 2.0f;
            } else {
                f = 2.0f;
                f2 = (LIZJ2 * 2.0f) + 0.0f;
            }
            canvas.drawBitmap(createScaledBitmap2, f2 + f13, (f11 - f13) / f, paint);
            canvas.drawText(valueOf, f10 - ((float) measureText), ((float) (i5 / 2)) - ((textPaint.descent() + textPaint.ascent()) / f), textPaint);
            n.LIZIZ(copy, "");
            copy.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(str4)));
            copy.recycle();
            MethodCollector.o(3362);
            return;
        }
        IllegalArgumentException illegalArgumentException = new IllegalArgumentException("path must not be exists");
        MethodCollector.o(3362);
        throw illegalArgumentException;
    }
}
