package me.arimelody.aridroid;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

public class ReleaseModel {
    private final String id;
    private final String title;
    private final String description;
    private final String type;
    private final Date releaseDate;
    private final String artworkURL;
    private final String buyname;
    private final String buylink;
    private final String copyright;
    private final URL copyrightURL;
    private final ArrayList<MusicCreditModel> credits;

    public ReleaseModel(String id, String title, String description, String type, Date releaseDate, String artwork, String buyname, String buylink, String copyright, URL copyrightURL, ArrayList<MusicCreditModel> credits) {
        this.id = id;
        this.title = title;
        this.description = description;
        this.type = type;
        this.releaseDate = releaseDate;
        this.artworkURL = artwork;
        this.buyname = buyname;
        this.buylink = buylink;
        this.copyright = copyright;
        this.copyrightURL = copyrightURL;
        this.credits = credits;
    }

    public ReleaseModel(JSONObject json) throws JSONException {
        this.id = json.getString("id");
        this.title = json.getString("title");
        this.description = json.optString("description");
        this.type = json.getString("type");

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.GERMANY);
        Date releaseDate;
        try {
            releaseDate = df.parse(json.getString("releaseDate"));
        } catch (Exception e) {
            Log.w(Constants.TAG, "Failed to parse release date for " + this.id);
            releaseDate = Date.from(Instant.EPOCH);
        }
        this.releaseDate = releaseDate;

        credits = new ArrayList<>();
        try {
            if (json.has("credits")) {
                JSONArray jsonArtists = json.getJSONArray("credits");
                for (int i = 0; i < jsonArtists.length(); i++) {
                    try {
                        JSONObject credit = jsonArtists.getJSONObject(i);
                        boolean primary = credit.getBoolean("primary");
                        ArtistModel artist = new ArtistModel(credit);
                        credits.add(new MusicCreditModel(
                                artist,
                                credit.optString("role"),
                                primary));
                    } catch (JSONException e) {
                        Log.w(Constants.TAG, "Failed to parse credit for " + this.id + " at index " + i);
                    }
                }
            } else if (json.has("artists")) {
                JSONArray jsonArtists = json.getJSONArray("artists");
                for (int i = 0; i < jsonArtists.length(); i++) {
                    try {
                        credits.add(new MusicCreditModel(new ArtistModel("", jsonArtists.getString(i), "", ""), "", true));
                    } catch (JSONException e2) {
                        Log.w(Constants.TAG, "Failed to parse credit for " + this.id + " at index " + i);
                    }
                }
            }
        } catch(JSONException e){
            Log.w(Constants.TAG, "Failed to get credits for " + this.id);
        }

        this.artworkURL = json.optString("artwork");
        this.buyname = json.optString("buyname");
        this.buylink = json.optString("buylink");
        this.copyright = json.optString("copyright");

        URL copyrightURL;
        try {
            copyrightURL = URI.create(json.getString("copyrightURL")).toURL();
        } catch (Exception e) {
            copyrightURL = null;
        }
        this.copyrightURL = copyrightURL;
    }

    public String getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public String getType() {
        return type;
    }

    public Date getReleaseDate() {
        return releaseDate;
    }

    public String getArtworkURL() {
        return artworkURL;
    }

    public String getBuyname() {
        return buyname;
    }

    public String getBuylink() {
        return buylink;
    }

    public String getCopyright() {
        return copyright;
    }

    public URL getCopyrightURL() {
        return copyrightURL;
    }

    public ArrayList<MusicCreditModel> getCredits() {
        return credits;
    }
}
