package ctbrec.sites.stripchat;

import com.iheartradio.m3u8.*;
import com.iheartradio.m3u8.data.MasterPlaylist;
import com.iheartradio.m3u8.data.Playlist;
import com.iheartradio.m3u8.data.PlaylistData;
import ctbrec.AbstractModel;
import ctbrec.Config;
import ctbrec.io.HttpException;
import ctbrec.recorder.download.RecordingProcess;
import ctbrec.recorder.download.StreamSource;
import ctbrec.recorder.download.hls.HlsdlDownload;
import ctbrec.recorder.download.hls.MergedFfmpegHlsDownload;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ExecutionException;

import static ctbrec.Model.State.*;
import static ctbrec.io.HttpConstants.*;
import static ctbrec.sites.stripchat.StripchatHttpClient.JSON;
import static java.nio.charset.StandardCharsets.UTF_8;

public class StripchatModel extends AbstractModel {
    private static final Logger LOG = LoggerFactory.getLogger(StripchatModel.class);

    private int[] resolution = new int[]{0, 0};
    private int modelId = 0;
    private boolean isVr = false;
    private transient JSONObject modelInfo;

    private transient Instant lastInfoRequest = Instant.EPOCH;

    @Override
    public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
        if (ignoreCache) {
            JSONObject jsonResponse = getModelInfo();
            if (jsonResponse.has("user")) {
                JSONObject user = jsonResponse.getJSONObject("user");
                String status = user.optString("status");
                mapOnlineState(status);
                if (isBanned(user)) {
                    LOG.debug("Model inactive or deleted: {}", getName());
                    setMarkedForLaterRecording(true);
                }
                modelId = user.optInt("id");
                isVr = user.optBoolean("isVr", false);
            }
        }
        return onlineState == ONLINE;
    }

    private boolean isBanned(JSONObject user) {
        boolean isDeleted = user.optBoolean("isDeleted", false);
        boolean isApprovedModel = user.optBoolean("isApprovedModel", true);
        return (!isApprovedModel || isDeleted);
    }

    private void mapOnlineState(String status) {
        switch (status) {
            case "public" -> setOnlineState(ONLINE);
            case "idle" -> setOnlineState(AWAY);
            case "private", "p2p", "groupShow", "virtualPrivate" -> setOnlineState(PRIVATE);
            case "off" -> setOnlineState(OFFLINE);
            default -> {
                LOG.debug("Unknown online state {} for model {}", status, getName());
                setOnlineState(OFFLINE);
            }
        }
    }

    private JSONObject getModelInfo() throws IOException {
        if (Objects.nonNull(modelInfo) && Duration.between(lastInfoRequest, Instant.now()).getSeconds() < 5) {
            return modelInfo;
        }
        lastInfoRequest = Instant.now();
        modelInfo = loadModelInfo();
        return modelInfo;
    }

    private JSONObject loadModelInfo() throws IOException {
        String name = getName();
        String url = getSite().getBaseUrl() + "/api/front/users/username/" + name;
        Request req = new Request.Builder()
                .url(url)
                .header(ACCEPT, MIMETYPE_APPLICATION_JSON)
                .header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
                .header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
                .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
                .header(REFERER, getUrl())
                .build();
        try (Response response = site.getHttpClient().execute(req)) {
            if (response.isSuccessful()) {
                JSONObject jsonResponse = new JSONObject(response.body().string());
                return jsonResponse;
            } else {
                throw new HttpException(response.code(), response.message());
            }
        }
    }

    @Override
    public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException {
        String url = getMasterPlaylistUrl();
        MasterPlaylist masterPlaylist = getMasterPlaylist(url);
        List<StreamSource> streamSources = extractStreamSources(masterPlaylist);
        try {
            String originalUrl = url.replace("_auto", "");
            masterPlaylist = getMasterPlaylist(originalUrl);
            for (StreamSource original : extractStreamSources(masterPlaylist)) {
                boolean found = false;
                for (StreamSource source : streamSources) {
                    if (source.height == original.height) {
                        found = true;
                    }
                }
                if (!found) streamSources.add(original);
            }
        } catch (Exception e) {
            LOG.warn("Original stream quality not available", e);
        }
        return streamSources;
    }

    private List<StreamSource> extractStreamSources(MasterPlaylist masterPlaylist) {
        List<StreamSource> sources = new ArrayList<>();
        for (PlaylistData playlist : masterPlaylist.getPlaylists()) {
            if (playlist.hasStreamInfo()) {
                StreamSource src = new StreamSource();
                src.bandwidth = playlist.getStreamInfo().getBandwidth();
                src.height = playlist.getStreamInfo().getResolution().height;
                src.mediaPlaylistUrl = playlist.getUri();
                if (src.mediaPlaylistUrl.contains("?")) {
                    src.mediaPlaylistUrl = src.mediaPlaylistUrl.substring(0, src.mediaPlaylistUrl.lastIndexOf('?'));
                }
                LOG.trace("Media playlist {}", src.mediaPlaylistUrl);
                sources.add(src);
            }
        }
        return sources;
    }

    private MasterPlaylist getMasterPlaylist(String url) throws IOException, ParseException, PlaylistException {
        LOG.trace("Loading master playlist {}", url);
        Request req = new Request.Builder()
                .url(url)
                .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
                .build();
        try (Response response = getSite().getHttpClient().execute(req)) {
            if (response.isSuccessful()) {
                String body = response.body().string();
                LOG.trace(body);
                InputStream inputStream = new ByteArrayInputStream(body.getBytes(UTF_8));
                PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
                Playlist playlist = parser.parse();
                MasterPlaylist master = playlist.getMasterPlaylist();
                return master;
            } else {
                throw new HttpException(response.code(), response.message());
            }
        }
    }

    private String getMasterPlaylistUrl() throws IOException {
        boolean VR = Config.getInstance().getSettings().stripchatVR;
        String hlsUrlTemplate = "https://edge-hls.doppiocdn.com/hls/{0}{1}/master/{0}{1}_auto.m3u8?playlistType=Standart";
        String vrSuffix = (VR && isVr) ? "_vr" : "";
        if (modelId > 0) {
            return MessageFormat.format(hlsUrlTemplate, String.valueOf(modelId), vrSuffix);
        }
        String name = getName();
        String url = getSite().getBaseUrl() + "/api/front/models/username/" + name + "/cam?triggerRequest=loadCam";
        Request req = new Request.Builder()
                .url(url)
                .header(ACCEPT, MIMETYPE_APPLICATION_JSON)
                .header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
                .header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
                .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
                .header(REFERER, getUrl())
                .build();
        try (Response response = site.getHttpClient().execute(req)) {
            if (response.isSuccessful()) {
                String body = response.body().string();
                LOG.trace(body);
                JSONObject jsonResponse = new JSONObject(body);
                String streamName = jsonResponse.optString("streamName");
                JSONObject broadcastSettings = jsonResponse.getJSONObject("broadcastSettings");
                String vrBroadcastServer = broadcastSettings.optString("vrBroadcastServer");
                vrSuffix = (!VR || vrBroadcastServer.isEmpty()) ? "" : "_vr";
                return MessageFormat.format(hlsUrlTemplate, streamName, vrSuffix);
            } else {
                throw new HttpException(response.code(), response.message());
            }
        }
    }


    @Override
    public void invalidateCacheEntries() {
        resolution = new int[]{0, 0};
        lastInfoRequest = Instant.EPOCH;
        modelInfo = null;
    }

    @Override
    public void receiveTip(Double tokens) throws IOException {
        // not implemented
    }

    @Override
    public int[] getStreamResolution(boolean failFast) throws ExecutionException {
        if (!failFast) {
            try {
                List<StreamSource> sources = getStreamSources();
                if (!sources.isEmpty()) {
                    StreamSource best = sources.get(sources.size() - 1);
                    resolution = new int[]{best.getWidth(), best.getHeight()};
                }
            } catch (IOException | ParseException | PlaylistException e) {
                throw new ExecutionException(e);
            }
        }
        return resolution;
    }

    @Override
    public boolean follow() throws IOException {
        getSite().getHttpClient().login();
        JSONObject modelInfo = getModelInfo();
        JSONObject user = modelInfo.getJSONObject("user");
        long modelId = user.optLong("id");

        StripchatHttpClient client = (StripchatHttpClient) getSite().getHttpClient();
        String url = Stripchat.baseUri + "/api/front/users/" + client.getUserId() + "/favorites/" + modelId;
        JSONObject requestParams = new JSONObject();
        requestParams.put("csrfToken", client.getCsrfToken());
        requestParams.put("csrfTimestamp", client.getCsrfTimestamp());
        requestParams.put("csrfNotifyTimestamp", client.getCsrfNotifyTimestamp());
        requestParams.put("uniq", getUniq());
        requestParams.put("ampl", client.getAmpl());
        RequestBody body = RequestBody.Companion.create(requestParams.toString(), JSON);
        Request request = new Request.Builder()
                .url(url)
                .header(ACCEPT, "*/*")
                .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
                .header(ORIGIN, Stripchat.baseUri)
                .header(REFERER, Stripchat.baseUri + '/' + getName())
                .header(CONTENT_TYPE, MIMETYPE_APPLICATION_JSON)
                .put(body)
                .build();
        try (Response response = client.execute(request)) {
            if (response.isSuccessful()) {
                return true;
            } else {
                throw new HttpException(response.code(), response.message());
            }
        }
    }

    @Override
    public boolean unfollow() throws IOException {
        getSite().getHttpClient().login();
        JSONObject modelInfo = getModelInfo();
        JSONObject user = modelInfo.getJSONObject("user");
        long modelId = user.optLong("id");
        JSONArray favoriteIds = new JSONArray();
        favoriteIds.put(modelId);

        StripchatHttpClient client = (StripchatHttpClient) getSite().getHttpClient();
        String url = Stripchat.baseUri + "/api/front/users/" + client.getUserId() + "/favorites";
        JSONObject requestParams = new JSONObject();
        requestParams.put("favoriteIds", favoriteIds);
        requestParams.put("csrfToken", client.getCsrfToken());
        requestParams.put("csrfTimestamp", client.getCsrfTimestamp());
        requestParams.put("csrfNotifyTimestamp", client.getCsrfNotifyTimestamp());
        requestParams.put("uniq", getUniq());
        RequestBody body = RequestBody.Companion.create(requestParams.toString(), JSON);
        Request request = new Request.Builder()
                .url(url)
                .header(ACCEPT, MIMETYPE_APPLICATION_JSON)
                .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
                .header(ORIGIN, Stripchat.baseUri)
                .header(REFERER, Stripchat.baseUri)
                .header(CONTENT_TYPE, MIMETYPE_APPLICATION_JSON)
                .delete(body)
                .build();
        try (Response response = client.execute(request)) {
            if (response.isSuccessful()) {
                return true;
            } else {
                throw new HttpException(response.code(), response.message());
            }
        }
    }

    @Override
    public boolean exists() throws IOException {
        Request req = new Request.Builder() // @formatter:off
                .url(getUrl())
                .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
                .build(); // @formatter:on
        try (Response response = getSite().getHttpClient().execute(req)) {
            if (!response.isSuccessful() && response.code() == 404) {
                return false;
            }
        }
        JSONObject jsonResponse = getModelInfo();
        if (jsonResponse.has("user")) {
            JSONObject user = jsonResponse.getJSONObject("user");
            if (isBanned(user)) {
                LOG.debug("Model inactive or deleted: {}", getName());
                return false;
            }
        }
        return true;
    }

    @Override
    public RecordingProcess createDownload() {
        if (Config.getInstance().getSettings().useHlsdl) {
            return new HlsdlDownload();
        } else {
            return new MergedFfmpegHlsDownload(getSite().getHttpClient());
        }
    }

    protected String getUniq() {
        Random r = new Random();
        String dict = "0123456789abcdefghijklmnopqarstvwxyz";
        char[] text = new char[16];
        for (int i = 0; i < 16; i++) {
            text[i] = dict.charAt(r.nextInt(dict.length()));
        }
        return new String(text);
    }

}
