package com.ss.android.ugc.aweme.comment.ui;

import X.0AA;
import X.0AP;
import X.0HH;
import X.1hK;
import X.9nD;
import X.9o5;
import X.JJ7;
import X.M12;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.ss.android.ugc.aweme.comment.model.Comment;
import com.ss.android.ugc.aweme.live.alphaplayer.Configuration;
import com.ss.android.ugc.aweme.live.alphaplayer.controller.PlayerController;
import com.ss.android.ugc.aweme.live.alphaplayer.model.DataSource;
import com.ss.android.ugc.aweme.live.alphaplayer.player.DefaultSystemPlayer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Objects;
import kotlin.h.b.n;
import org.json.JSONObject;

public final class GiftAnimationFragment extends DialogFragment {
    public static final 9o5 LIZIZ = new 9o5((byte) 0);
    public PlayerController LIZ;
    public Comment LIZJ;
    public SparseArray LIZLLL;

    static {
        Covode.recordClassIndex(57357);
    }

    public final void onDestroyView() {
        PlayerController playerController = this.LIZ;
        if (playerController != null) {
            playerController.release();
        }
        GiftAnimationFragment.super.onDestroyView();
        SparseArray sparseArray = this.LIZLLL;
        if (sparseArray != null) {
            sparseArray.clear();
        }
    }

    public final void LIZ() {
        0AA supportFragmentManager;
        Fragment LIZ2;
        1hK activity;
        0AA supportFragmentManager2;
        1hK activity2 = getActivity();
        if (activity2 != null && (supportFragmentManager = activity2.getSupportFragmentManager()) != null && (LIZ2 = supportFragmentManager.LIZ("GiftAnimationFragment")) != null && (activity = getActivity()) != null && (supportFragmentManager2 = activity.getSupportFragmentManager()) != null) {
            0AP LIZ3 = supportFragmentManager2.LIZ();
            LIZ3.LIZ(LIZ2);
            LIZ3.LIZIZ();
        }
    }

    public final void onStart() {
        GiftAnimationFragment.super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                window.clearFlags(2);
            }
            Window window2 = dialog.getWindow();
            if (window2 != null) {
                window2.setBackgroundDrawable(new ColorDrawable(0));
            }
            Window window3 = dialog.getWindow();
            if (window3 == null) {
                n.LIZIZ();
            }
            window3.setLayout(-1, -1);
        }
    }

    public final void onCreate(Bundle bundle) {
        GiftAnimationFragment.super.onCreate(bundle);
        if (getArguments() != null) {
            Bundle arguments = getArguments();
            if (arguments == null) {
                n.LIZIZ();
            }
            Comment serializable = arguments.getSerializable("comment");
            Objects.requireNonNull(serializable, "null cannot be cast to non-null type com.ss.android.ugc.aweme.comment.model.Comment");
            this.LIZJ = serializable;
        }
    }

    public final void show(0AA r1, String str) {
        GiftAnimationFragment.super.show(r1, str);
    }

    public final void onViewCreated(View view, Bundle bundle) {
        String str;
        Long l;
        String str2;
        String str3;
        MethodCollector.i(9114);
        View view2 = view;
        M12.LIZ(view2);
        GiftAnimationFragment.super.onViewCreated(view2, bundle);
        if (this.LIZJ == null) {
            LIZ();
        }
        Comment comment = this.LIZJ;
        View view3 = null;
        if (comment != null) {
            str = comment.getGiftEffectPath();
        } else {
            str = null;
        }
        Comment comment2 = this.LIZJ;
        if (comment2 != null) {
            l = Long.valueOf(comment2.getFakeGiftId());
        } else {
            l = null;
        }
        if (9nD.LIZ(str) || (l != null && l.longValue() == 0)) {
            LIZ();
        }
        DataSource.ScaleType scaleType = DataSource.ScaleType.TopFit;
        DataSource.ScaleType scaleType2 = DataSource.ScaleType.TopFit;
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(str, "config.json")));
            StringBuilder sb = new StringBuilder();
            for (String readLine = bufferedReader.readLine(); readLine != null; readLine = bufferedReader.readLine()) {
                sb.append(readLine);
                sb.append("\n");
            }
            bufferedReader.close();
            String sb2 = sb.toString();
            n.LIZIZ(sb2, "");
            JSONObject jSONObject = new JSONObject(sb2);
            str2 = new JSONObject(jSONObject.get("landscape").toString()).getString("path");
            n.LIZIZ(str2, "");
            try {
                str3 = new JSONObject(jSONObject.get("portrait").toString()).getString("path");
                n.LIZIZ(str3, "");
                try {
                    scaleType = DataSource.ScaleType.convertFrom(new JSONObject(jSONObject.get("landscape").toString()).getInt("align"));
                    scaleType2 = DataSource.ScaleType.convertFrom(new JSONObject(jSONObject.get("portrait").toString()).getInt("align"));
                } catch (Exception unused) {
                }
            } catch (Exception unused2) {
                str3 = "";
            }
        } catch (Exception unused3) {
            str3 = "";
            str2 = str3;
        }
        DataSource dataSource = new DataSource();
        DataSource.DataInfo dataInfo = new DataSource.DataInfo(n.LIZ(str, str2));
        dataInfo.setScaleType(scaleType);
        dataSource.setLandscapeDataInfo(dataInfo);
        DataSource.DataInfo dataInfo2 = new DataSource.DataInfo(n.LIZ(str, str3));
        dataInfo2.setScaleType(scaleType2);
        dataSource.setPortraitDataInfo(dataInfo2);
        Configuration configuration = new Configuration();
        configuration.setContext(getContext());
        configuration.setLifecycleOwner(getActivity());
        try {
            if (getView() != null) {
                View view4 = getView();
                if (view4 == null) {
                    n.LIZIZ();
                }
                n.LIZIZ(view4, "");
                if (view4.getContext() != null && (view3 = view2.findViewById(2131362555)) == null) {
                    LIZ();
                }
            }
        } catch (Exception unused4) {
            LIZ();
        }
        PlayerController playerController = this.LIZ;
        if (playerController != null) {
            playerController.detachAlphaView((ViewGroup) view3);
            PlayerController playerController2 = this.LIZ;
            if (playerController2 == null) {
                n.LIZIZ();
            }
            playerController2.release();
        }
        PlayerController playerController3 = PlayerController.get(configuration, new DefaultSystemPlayer());
        this.LIZ = playerController3;
        if (playerController3 == null) {
            n.LIZIZ();
        }
        playerController3.attachAlphaView((ViewGroup) view3);
        PlayerController playerController4 = this.LIZ;
        if (playerController4 == null) {
            n.LIZIZ();
        }
        playerController4.withVideoAction(new JJ7(this));
        PlayerController playerController5 = this.LIZ;
        if (playerController5 == null) {
            n.LIZIZ();
        }
        playerController5.start(dataSource);
        MethodCollector.o(9114);
    }

    public final View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        M12.LIZ(layoutInflater);
        setCancelable(false);
        return 0HH.LIZ(layoutInflater, 2131558791, viewGroup, false);
    }
}
