package fishrungames.bashgid.core;

import fishrungames.networkutils.ImageManager;


import java.util.ArrayList;

import java.util.Locale;


import fishrungames.networkutils.DownloadFunctions;

import fishrungames.bashgid.MainActivity;
import fishrungames.bashgid.R;
import fishrungames.bashgid.core.ArticleManager.ArticleRecordData;
import fishrungames.bashgid.core.TranslationManager.TranslationRecordData;


public class NewsManager {

	/*
	public static final String [] urlArr = {
		"https://www.bashkortostan.ru/presscenter/news/rss/",
		"http://www.bashedu.ru/rss.xml",
		"http://www.bashinform.ru/rss/all.xml",
		"http://www.minkult-rb.ru/news/rss/",
		"http://02.mvd.ru/news/rss/"
		}; */
	
	public static final String [] urlArr = {
		"https://news.yandex.ru/Ufa/index.rss",
		};
	
	
	public static ArrayList<String> ExtractEnclosureImageUrl(ArrayList<RssReader.Rss2Item> rss2ItemArr)
	{
		ArrayList<String> imageToDownloadList = new ArrayList<String>();
		
		for (RssReader.Rss2Item rss2Item : rss2ItemArr)
		{
			//Enclosure
			if (rss2Item.rss2Enclosure != null)
			{
				if (DownloadFunctions.MimeTypeIsImage(rss2Item.rss2Enclosure.mimeType))
				{
					imageToDownloadList.add(rss2Item.rss2Enclosure.url);
				}
			}
		}
		
		return imageToDownloadList;
	}
	
	public static ArrayList<String> ExtractDescriptionImageUrl(ArrayList<RssReader.Rss2Item> rss2ItemArr)
	{
		ArrayList<String> imageToDownloadList = new ArrayList<String>();
		
		for (RssReader.Rss2Item rss2Item : rss2ItemArr)
		{
			//And images inside text
			int i = rss2Item.description.indexOf("<img");
			
			while (i != -1)
			{
				int j = rss2Item.description.indexOf("src=\"", i);
				
				if (j != -1)
				{
					j += ("src=\"").length();
					
					int k = rss2Item.description.indexOf("\"", j);
					
					String imageUrl = rss2Item.description.substring(j, k);
		
					imageToDownloadList.add(imageUrl);
				}
				
				i = rss2Item.description.indexOf("<img", j);
			}
		}
		
		return imageToDownloadList;
	}
	
	public static ArrayList<String> ConvertRelativeToAbsoluteImageUrlList(ArrayList<String> relativeImageUrlList, String url)
	{
		ArrayList<String> absoluteImageUrlList = new ArrayList<String>();
		
		for (String relativeImageUrl : relativeImageUrlList)
		{
			if (!relativeImageUrl.startsWith("http://"))
			{
				String homeFolder = DownloadFunctions.GetUrlFolder(url);
				
				relativeImageUrl = homeFolder + relativeImageUrl;
			}
			
			absoluteImageUrlList.add(relativeImageUrl);
		}
		
		return absoluteImageUrlList;
	}
	
	
	static void ReplaceRemoteUrlToLocalUrl(ArrayList<RssReader.Rss2Item> rss2ItemArr, ArrayList<String> relativeImageUrlList, ArrayList<String> absoluteImageUrlList)
	{

		for (RssReader.Rss2Item rss2Item : rss2ItemArr)
		{
			for (int i = 0; i < relativeImageUrlList.size(); i++)
			{
				String fileName = ImageManager.GetImageLocalUrl(absoluteImageUrlList.get(i));
			
				rss2Item.description = rss2Item.description.replace("\"" +relativeImageUrlList.get(i) + "\"" , "\"" +fileName + "\"");
			}
		}
	}
	
	static void LoadNewsAndImagesFromRss(String url)
	{
		//Xperimental -- need to optimize this code. slow block BEGINS
		String xmlCode = DownloadFunctions.getXmlFromUrl(url);
		
		if (xmlCode == null)
		{
			return;
		}
		
		//Remove BOM character if present
		if (xmlCode.startsWith("\uFEFF"))
		{
			xmlCode = xmlCode.substring(1);
	    }
		
		RssReader rssReader = new RssReader();
		
		rssReader.ReadRss(xmlCode);
		
				
		
		//Xperimental -- need to optimize this code. slow block ENDS
		
		
		for (RssReader.Rss2Item rss2Item : rssReader.rss2ItemArr)
		{
	
			String title = rss2Item.title.replace("'", "''").replace("\"", "\"\"");
			String description = rss2Item.description.replace("'", "''").replace("\"", "\"\"");
			
			String titleUp = title.toUpperCase(new Locale("ru"));
			String descriptionUp = description.toUpperCase(new Locale("ru"));
			
			
		ArticleRecordData newsArticle = new ArticleRecordData(
				title,
				ArticleManager.ARTICLE_TYPE_NEWS,
				title,
				description,
				0,
				0,
				"",
				"",
				rss2Item.link,
				MainActivity.getInstance().getResources().getString(R.string.dialog_open),
				rss2Item.pubDate,
				0);
		
		AlbumManager.AddNewArticleToJournal(newsArticle, "news");
		
		//To perform search
		MainActivity.getInstance().translationDataSource.CreateNewTranslation(new TranslationRecordData(
				title,
				title, //En
				title, //Ru
				title, //Zh
				titleUp, //En Up
				titleUp, //Ru Up
				titleUp  //Zh Up
				));
		
		MainActivity.getInstance().translationDataSource.CreateNewTranslation(new TranslationRecordData(
				description,
				description, //En
				description, //Ru
				description, //Zh
				descriptionUp, //En Up
				descriptionUp, //Ru Up
				descriptionUp  //Zh Up
				));
		
		}
		
	}
}
