package com.puntoz.pro;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.media3.common.MediaItem;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.DefaultHttpDataSource;
import androidx.media3.exoplayer.DefaultLoadControl;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.hls.HlsMediaSource;
import androidx.media3.exoplayer.source.ProgressiveMediaSource;
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector;
import androidx.media3.ui.AspectRatioFrameLayout;
import androidx.media3.ui.PlayerView;

import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import java.net.HttpURLConnection;
import java.net.URLEncoder;

@UnstableApi
public class PlayerActivity extends Activity {

    PlayerView playerView;
    ExoPlayer player;
    Handler handler = new Handler();

    FrameLayout root;
    LinearLayout osdBox;
    ImageView logoView;
    TextView nowView;
    TextView nowTimeView;
    TextView nextView;
    TextView nextTimeView;
    TextView remainView;
    View progressDone;
    View progressLeft;

    String url = "";
    String channelName = "Puntoz TV";
    String channelLogo = "";
    String nowTitle = "No information";
    String nextTitle = "No information";
    String startTime = "";
    String endTime = "";
    String nextStartTime = "";
    String nextEndTime = "";
    String streamId = "";

    ArrayList<EpgItem> epgItems = new ArrayList<>();
    Runnable epgTicker;
    Runnable osdHideTicker;
    boolean epgReloading = false;
    long lastEpgReload = 0;

    int amarillo = Color.rgb(255, 220, 0);
    int azul = Color.rgb(0, 210, 255);

    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        url = getIntent().getStringExtra("url");
        channelName = getIntent().getStringExtra("channel_name");
        channelLogo = getIntent().getStringExtra("channel_logo");
        nowTitle = getIntent().getStringExtra("now_title");
        nextTitle = getIntent().getStringExtra("next_title");
        startTime = getIntent().getStringExtra("start_time");
        endTime = getIntent().getStringExtra("end_time");
        nextStartTime = getIntent().getStringExtra("next_start_time");
        nextEndTime = getIntent().getStringExtra("next_end_time");
        streamId = getIntent().getStringExtra("stream_id");

        if (url == null) url = "";
        if (channelName == null || channelName.trim().isEmpty()) channelName = "Puntoz TV";
        if (channelLogo == null) channelLogo = "";
        if (nowTitle == null || nowTitle.trim().isEmpty()) nowTitle = "No information";
        if (nextTitle == null || nextTitle.trim().isEmpty()) nextTitle = "No information";
        if (startTime == null) startTime = "";
        if (endTime == null) endTime = "";
        if (nextStartTime == null) nextStartTime = "";
        if (nextEndTime == null) nextEndTime = "";
        if (streamId == null) streamId = "";

        root = new FrameLayout(this);

        playerView = new PlayerView(this);
        playerView.setUseController(false);
        playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);

        root.addView(playerView, new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT
        ));

        crearOSD();

        setContentView(root);

        DefaultTrackSelector selector = new DefaultTrackSelector(this);
        selector.setParameters(selector.buildUponParameters()
                .setPreferredAudioLanguage("es")
                .setPreferredTextLanguage("es")
        );

        DefaultLoadControl loadControl = new DefaultLoadControl.Builder()
                .setBufferDurationsMs(8000, 50000, 1500, 3000)
                .build();

        player = new ExoPlayer.Builder(this)
                .setTrackSelector(selector)
                .setLoadControl(loadControl)
                .build();

        playerView.setPlayer(player);
        playUrl(url);
    }

    private TextView tv(String text, int size, int color) {
        TextView t = new TextView(this);
        t.setText(text);
        t.setTextColor(color);
        t.setTextSize(size);
        t.setIncludeFontPadding(false);
        return t;
    }

    private SpannableString labelColor(String label, String text, int color) {
        SpannableString sp = new SpannableString(label + text);
        sp.setSpan(new ForegroundColorSpan(color), 0, label.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sp;
    }

    private TextView chip(String txt) {
        TextView t = tv(txt, 13, Color.LTGRAY);
        t.setGravity(Gravity.CENTER);
        GradientDrawable bg = new GradientDrawable();
        bg.setColor(Color.argb(35, 0, 0, 0));
        bg.setStroke(2, Color.argb(160, 180, 180, 180));
        bg.setCornerRadius(8);
        t.setBackground(bg);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(68, 30);
        lp.setMargins(8, 0, 8, 0);
        t.setLayoutParams(lp);
        return t;
    }

    private void crearOSD() {
        osdBox = new LinearLayout(this);
        osdBox.setOrientation(LinearLayout.HORIZONTAL);
        osdBox.setGravity(Gravity.CENTER_VERTICAL);
        osdBox.setPadding(28, 16, 28, 16);
        osdBox.setVisibility(View.GONE);

        GradientDrawable bg = new GradientDrawable();
        bg.setColor(Color.argb(82, 0, 0, 0));
        bg.setStroke(2, Color.argb(80, 255, 255, 255));
        bg.setCornerRadius(16);
        osdBox.setBackground(bg);

        logoView = new ImageView(this);
        logoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        LinearLayout.LayoutParams logoLp = new LinearLayout.LayoutParams(220, 150);
        logoLp.setMargins(0, 0, 28, 0);
        osdBox.addView(logoView, logoLp);

        LinearLayout info = new LinearLayout(this);
        info.setOrientation(LinearLayout.VERTICAL);
        info.setPadding(0, 18, 0, 0);

        nowView = tv("", 13, Color.WHITE);
        nowView.setText(labelColor("Ahora: ", nowTitle, amarillo));

        nowTimeView = tv(formatoHorario(startTime, endTime), 11, Color.WHITE);

        LinearLayout progressRow = new LinearLayout(this);
        progressRow.setOrientation(LinearLayout.HORIZONTAL);
        progressRow.setGravity(Gravity.CENTER_VERTICAL);

        LinearLayout bar = new LinearLayout(this);
        bar.setOrientation(LinearLayout.HORIZONTAL);
        GradientDrawable barBg = new GradientDrawable();
        barBg.setColor(Color.argb(120, 160, 170, 180));
        barBg.setCornerRadius(6);
        bar.setBackground(barBg);

        int progress = calcularProgreso(startTime, endTime);

        progressDone = new View(this);
        progressDone.setBackgroundColor(amarillo);
        bar.addView(progressDone, new LinearLayout.LayoutParams(0, 7, progress));

        progressLeft = new View(this);
        progressLeft.setBackgroundColor(Color.TRANSPARENT);
        bar.addView(progressLeft, new LinearLayout.LayoutParams(0, 7, Math.max(1, 100 - progress)));

        LinearLayout.LayoutParams barLp = new LinearLayout.LayoutParams(560, 7);
        barLp.setMargins(0, 1, 14, 1);
        progressRow.addView(bar, barLp);

        remainView = tv(minutosRestantes(endTime), 11, Color.WHITE);
        progressRow.addView(remainView);

        nextView = tv("", 13, Color.WHITE);
        nextView.setText(labelColor("Sigue: ", nextTitle, azul));

        nextTimeView = tv(formatoHorario(nextStartTime, nextEndTime), 12, Color.WHITE);

        info.addView(nowView);
        info.addView(nowTimeView);
        info.addView(progressRow);
        info.addView(nextView);
        info.addView(nextTimeView);

        osdBox.addView(info, new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1));

        View divider = new View(this);
        divider.setBackgroundColor(Color.argb(130, 180, 180, 180));
        LinearLayout.LayoutParams divLp = new LinearLayout.LayoutParams(2, 128);
        divLp.setMargins(35, 0, 35, 0);
        osdBox.addView(divider, divLp);

        LinearLayout right = new LinearLayout(this);
        right.setOrientation(LinearLayout.HORIZONTAL);
        right.setGravity(Gravity.CENTER);
        right.addView(chip("FHD"));
        right.addView(chip("16:9"));
        right.addView(chip("STEREO"));

        osdBox.addView(right, new LinearLayout.LayoutParams(280, LinearLayout.LayoutParams.WRAP_CONTENT));

        FrameLayout.LayoutParams osdParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT
