package de.danoeh.antennapod.ui.share;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.ShareCompat;
import androidx.core.content.FileProvider;

import de.danoeh.antennapod.ui.common.Converter;
import java.io.File;
import java.net.URLEncoder;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.model.feed.Feed;
import de.danoeh.antennapod.model.feed.FeedItem;
import de.danoeh.antennapod.model.feed.FeedMedia;

/** Utility methods for sharing data */
public class ShareUtils {
    private static final String TAG = "ShareUtils";

    private ShareUtils() {
    }

    public static void shareLink(@NonNull Context context, @NonNull String text) {
        Intent intent = new ShareCompat.IntentBuilder(context)
                .setType("text/plain")
                .setText(text)
                .setChooserTitle(R.string.share_url_label)
                .createChooserIntent();
        context.startActivity(intent);
    }

    public static void shareFeedLink(Context context, Feed feed) {
        String text = feed.getTitle()
                + "\n\n"
                + "https://antennapod.org/deeplink/subscribe/?url="
                + URLEncoder.encode(feed.getDownloadUrl())
                + "&title="
                + URLEncoder.encode(feed.getTitle());
        shareLink(context, text);
    }

    public static boolean hasLinkToShare(FeedItem item) {
        return item.getLinkWithFallback() != null;
    }

    public static void shareMediaDownloadLink(Context context, FeedMedia media) {
        shareLink(context, media.getDownloadUrl());
    }

    public static void shareFeedItemLinkWithDownloadLink(Context context, FeedItem item, boolean withPosition) {
        String text = item.getFeed().getTitle() + ": " + item.getTitle();
        int pos = 0;
        if (item.getMedia() != null && withPosition) {
            text += "\n" + context.getResources().getString(R.string.share_starting_position_label) + ": ";
            pos = item.getMedia().getPosition();
            text +=  Converter.getDurationStringLong(pos);
        }

        if (hasLinkToShare(item)) {
            text +=  "\n\n" + context.getResources().getString(R.string.share_dialog_episode_website_label) + ": ";
            text += item.getLinkWithFallback();
        }

        if (item.getMedia() != null && item.getMedia().getDownloadUrl() != null) {
            text += "\n\n" + context.getResources().getString(R.string.share_dialog_media_file_label) + ": ";
            text +=  item.getMedia().getDownloadUrl();
            if (withPosition) {
                text += "#t=" + pos / 1000;
            }
        }
        shareLink(context, text);
    }

    public static void shareFeedItemFile(Context context, FeedMedia media) {
        Uri fileUri = FileProvider.getUriForFile(context, context.getString(R.string.provider_authority),
                new File(media.getLocalFileUrl()));

        new ShareCompat.IntentBuilder(context)
                .setType(media.getMimeType())
                .addStream(fileUri)
                .setChooserTitle(R.string.share_file_label)
                .startChooser();

        Log.e(TAG, "shareFeedItemFile called");
    }
}
