<commit_before>package com.chickenkiller.upods2.models;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

import com.chickenkiller.upods2.controllers.app.UpodsApplication;
import com.chickenkiller.upods2.interfaces.IMediaItemView;
import com.chickenkiller.upods2.interfaces.IOperationFinishCallback;
import com.chickenkiller.upods2.utils.GlobalUtils;
import com.chickenkiller.upods2.utils.Logger;
import com.chickenkiller.upods2.utils.MediaUtils;

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by alonzilberman on 7/3/15.
 */
public class RadioItem extends MediaItem {

    public static final String TABLE = "radio_stations";
    private final static String RADIO_LOG = "RADIO_LOG";

    protected Set<StreamUrl> streamUrls;
    protected StreamUrl selectedStreamUrl;
    protected String bannerImageUrl;
    protected String description;
    protected String website;
    protected String facebook;
    protected String twitter;
    protected String country;
    protected String genre;

    //Not in DB
    public boolean isRecent;

    public RadioItem() {
        super();
        this.name = "";
        this.selectedStreamUrl = null;
        this.streamUrls = new HashSet<>();
        this.coverImageUrl = "";
        this.bannerImageUrl = "";
        this.website = "";
        this.facebook = "";
        this.twitter = "";
        this.country = "";
        this.genre = "";
    }

    public RadioItem(String name, StreamUrl streamUrl, String coverImageUrl) {
        this();
        this.name = name;
        this.name = this.name.replace("\n", "").trim();
        this.streamUrls.add(streamUrl);
        this.coverImageUrl = coverImageUrl;
    }

    public RadioItem(RadioItem item) {
        this.id = item.id;
        this.name = item.name;
        this.name = this.name.replace("\n", "").trim();
        this.selectedStreamUrl = item.selectedStreamUrl;
        this.streamUrls = new HashSet<>(item.streamUrls);
        this.coverImageUrl = item.coverImageUrl;
        this.bannerImageUrl = item.bannerImageUrl;
        this.description = item.description;
        this.website = item.website;
        this.facebook = item.facebook;
        this.twitter = item.twitter;
        this.country = item.country;
        this.isExistsInDb = item.isExistsInDb;
        this.isRecent = item.isRecent;
        this.isSubscribed = item.isSubscribed;
    }

    public long save() {
        SQLiteDatabase database = UpodsApplication.getDatabaseManager().getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("description", description);
        values.put("website", website);
        values.put("facebook", facebook);
        values.put("twitter", twitter);
        values.put("cover_image_url", coverImageUrl);
        values.put("country", country);
        values.put("genre", genre);

        id = database.insert(TABLE, null, values);
        isExistsInDb = true;
        StreamUrl.saveSet(streamUrls, id);

        return id;
    }

    public RadioItem(JSONObject jsonItem) {
        try {
            this.streamUrls = new HashSet<>();
            this.id = jsonItem.has("id") ? jsonItem.getInt("id") : 0;
            this.name = jsonItem.has("name") ? jsonItem.getString("name") : "";
            this.name = name.replace("\n", "").trim();
            this.description = jsonItem.has("description") ? jsonItem.getString("description") : "";
            this.website = jsonItem.has("website") ? jsonItem.getString("website") : "";
            this.facebook = jsonItem.has("facebook") ? jsonItem.getString("facebook") : "";
            this.twitter = jsonItem.has("twitter") ? jsonItem.getString("twitter") : "";
            this.country = jsonItem.has("country") ? jsonItem.getString("country") : "";
            this.selectedStreamUrl = jsonItem.has("selectedStreamUrl") ? new StreamUrl(jsonItem.getJSONObject("selectedStreamUrl")) : null;

            if (jsonItem.has("streamUrls") && jsonItem.getJSONArray("streamUrls").length() > 0) {
                JSONArray jsonStreamUrls = jsonItem.getJSONArray("streamUrls");
                for (int i = 0; i < jsonStreamUrls.length(); i++) {
                    this.streamUrls.add(new StreamUrl(jsonStreamUrls.getJSONObject(i)));
                }
            }

            if (!jsonItem.has("covers")) {//from profile
                this.coverImageUrl = jsonItem.has("cover_image_url") ? jsonItem.getString("cover_image_url") : "";
                this.bannerImageUrl = jsonItem.has("banner_image_url") ? jsonItem.getString("banner_image_url") : "";
                this.genre = jsonItem.has("genre") ? jsonItem.getString("genre") : "";

            } else {//from backend
                if (jsonItem.has("covers") && jsonItem.getJSONArray("covers").length() > 0) {
                    this.coverImageUrl = jsonItem.getJSONArray("covers").getString(0);
                }
                if (jsonItem.has("banners") && jsonItem.getJSONArray("banners").length() > 0) {
                    this.bannerImageUrl = jsonItem.getJSONArray("banners").getString(0);
                }
                if (jsonItem.has("genres") && jsonItem.getJSONArray("genres").length() > 0) {
                    this.genre = jsonItem.getJSONArray("genres").getString(0);
                }

            }
            this.name = this.name.replace("\n", "").trim();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getAudeoLink() {
        if (selectedStreamUrl != null) {
            return selectedStreamUrl.getUrl();
        }
        return StreamUrl.getBestStreamUrl(streamUrls).getUrl();
    }

    @Override
    public String getBitrate() {
        if (selectedStreamUrl != null) {
            return selectedStreamUrl.getBitrate();
        }
        return StreamUrl.getBestStreamUrl(streamUrls).getBitrate();
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getCoverImageUrl() {
        return coverImageUrl != null && coverImageUrl.isEmpty() ? null : coverImageUrl;
    }

    @Override
    public String getSubHeader() {
        return this.country;
    }

    @Override
    public String getDescription() {
        return description;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFacebook() {
        return facebook;
    }

    public void setFacebook(String facebook) {
        this.facebook = facebook;
    }

    public String getTwitter() {
        return twitter;
    }

    public String getCountry() {
        return country;
    }

    public String getBannerImageUrl() {
        return bannerImageUrl;<commit_msg>Fix for midle scree crash.<commit_after>package com.chickenkiller.upods2.models;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

import com.chickenkiller.upods2.controllers.app.UpodsApplication;
import com.chickenkiller.upods2.interfaces.IMediaItemView;
import com.chickenkiller.upods2.interfaces.IOperationFinishCallback;
import com.chickenkiller.upods2.utils.GlobalUtils;
import com.chickenkiller.upods2.utils.Logger;
import com.chickenkiller.upods2.utils.MediaUtils;

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by alonzilberman on 7/3/15.
 */
public class RadioItem extends MediaItem {

    public static final String TABLE = "radio_stations";
    private final static String RADIO_LOG = "RADIO_LOG";

    protected Set<StreamUrl> streamUrls;
    protected StreamUrl selectedStreamUrl;
    protected String bannerImageUrl;
    protected String description;
    protected String website;
    protected String facebook;
    protected String twitter;
    protected String country;
    protected String genre;

    //Not in DB
    public boolean isRecent;

    public RadioItem() {
        super();
        this.name = "";
        this.description = "";
        this.selectedStreamUrl = null;
        this.streamUrls = new HashSet<>();
        this.coverImageUrl = "";
        this.bannerImageUrl = "";
        this.website = "";
        this.facebook = "";
        this.twitter = "";
        this.country = "";
        this.genre = "";
    }

    public RadioItem(String name, StreamUrl streamUrl, String coverImageUrl) {
        this();
        this.name = name;
        this.name = this.name.replace("\n", "").trim();
        this.streamUrls.add(streamUrl);
        this.coverImageUrl = coverImageUrl;
    }

    public RadioItem(RadioItem item) {
        this.id = item.id;
        this.name = item.name;
        this.name = this.name.replace("\n", "").trim();
        this.selectedStreamUrl = item.selectedStreamUrl;
        this.streamUrls = new HashSet<>(item.streamUrls);
        this.coverImageUrl = item.coverImageUrl;
        this.bannerImageUrl = item.bannerImageUrl;
        this.description = item.description;
        this.website = item.website;
        this.facebook = item.facebook;
        this.twitter = item.twitter;
        this.country = item.country;
        this.isExistsInDb = item.isExistsInDb;
        this.isRecent = item.isRecent;
        this.isSubscribed = item.isSubscribed;
    }

    public long save() {
        SQLiteDatabase database = UpodsApplication.getDatabaseManager().getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("description", description);
        values.put("website", website);
        values.put("facebook", facebook);
        values.put("twitter", twitter);
        values.put("cover_image_url", coverImageUrl);
        values.put("country", country);
        values.put("genre", genre);

        id = database.insert(TABLE, null, values);
        isExistsInDb = true;
        StreamUrl.saveSet(streamUrls, id);

        return id;
    }

    public RadioItem(JSONObject jsonItem) {
        try {
            this.streamUrls = new HashSet<>();
            this.id = jsonItem.has("id") ? jsonItem.getInt("id") : 0;
            this.name = jsonItem.has("name") ? jsonItem.getString("name") : "";
            this.name = name.replace("\n", "").trim();
            this.description = jsonItem.has("description") ? jsonItem.getString("description") : "";
            this.website = jsonItem.has("website") ? jsonItem.getString("website") : "";
            this.facebook = jsonItem.has("facebook") ? jsonItem.getString("facebook") : "";
            this.twitter = jsonItem.has("twitter") ? jsonItem.getString("twitter") : "";
            this.country = jsonItem.has("country") ? jsonItem.getString("country") : "";
            this.selectedStreamUrl = jsonItem.has("selectedStreamUrl") ? new StreamUrl(jsonItem.getJSONObject("selectedStreamUrl")) : null;

            if (jsonItem.has("streamUrls") && jsonItem.getJSONArray("streamUrls").length() > 0) {
                JSONArray jsonStreamUrls = jsonItem.getJSONArray("streamUrls");
                for (int i = 0; i < jsonStreamUrls.length(); i++) {
                    this.streamUrls.add(new StreamUrl(jsonStreamUrls.getJSONObject(i)));
                }
            }

            if (!jsonItem.has("covers")) {//from profile
                this.coverImageUrl = jsonItem.has("cover_image_url") ? jsonItem.getString("cover_image_url") : "";
                this.bannerImageUrl = jsonItem.has("banner_image_url") ? jsonItem.getString("banner_image_url") : "";
                this.genre = jsonItem.has("genre") ? jsonItem.getString("genre") : "";

            } else {//from backend
                if (jsonItem.has("covers") && jsonItem.getJSONArray("covers").length() > 0) {
                    this.coverImageUrl = jsonItem.getJSONArray("covers").getString(0);
                }
                if (jsonItem.has("banners") && jsonItem.getJSONArray("banners").length() > 0) {
                    this.bannerImageUrl = jsonItem.getJSONArray("banners").getString(0);
                }
                if (jsonItem.has("genres") && jsonItem.getJSONArray("genres").length() > 0) {
                    this.genre = jsonItem.getJSONArray("genres").getString(0);
                }

            }
            this.name = this.name.replace("\n", "").trim();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getAudeoLink() {
        if (selectedStreamUrl != null) {
            return selectedStreamUrl.getUrl();
        }
        return StreamUrl.getBestStreamUrl(streamUrls).getUrl();
    }

    @Override
    public String getBitrate() {
        if (selectedStreamUrl != null) {
            return selectedStreamUrl.getBitrate();
        }
        return StreamUrl.getBestStreamUrl(streamUrls).getBitrate();
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getCoverImageUrl() {
        return coverImageUrl != null && coverImageUrl.isEmpty() ? null : coverImageUrl;
    }

    @Override
    public String getSubHeader() {
        return this.country;
    }

    @Override
    public String getDescription() {
        return description != null ? description : "";
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFacebook() {
        return facebook;
    }

    public void setFacebook(String facebook) {
        this.facebook = facebook;
    }

    public String getTwitter() {
        return twitter;
    }

    public String getCountry() {
        return country;
    }

    public String getBannerImageUrl() {
        return bannerImageUrl;
{'fraction_non_alphanumeric': 0.12458693665190185, 'fraction_numerical': 0.0028123461998171974, 'mean_word_length': 2.522535908865775, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '677809', 'n_tokens_mistral': 4105, 'n_tokens_neox': 3936, 'n_words': 758}