package dev.openshift.tapestry.angular.services;

import java.io.*;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;


import dev.openshift.tapestry.angular.data.Comment;
import dev.openshift.tapestry.angular.data.Phone;
import dev.openshift.tapestry.angular.data.PhoneDetails;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;




public class PhoneCatalogImpl implements PhoneCatalog
{

    private List<Phone> catalog;

    private Hashtable<String, PhoneDetails> hPhoneDetails;

    private StringBuilder phonesStrBuilder;

    private Hashtable<String, String> hPhoneDetailsJSON;

    private List<Comment> comments;

    private final String path = "dev/openshift/tapestry/angular/pages/phones/";

    private int commentInc;

    public PhoneCatalogImpl() {

        commentInc = 0;

        try {

            comments = new ArrayList<Comment>();

            final String filePath = path + "phones.json";
            // read the json file
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            InputStream inputStream = classLoader.getResourceAsStream(filePath);

            BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            phonesStrBuilder = new StringBuilder();

            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                    phonesStrBuilder.append(inputStr);

            final ObjectMapper mapper = new ObjectMapper();


            catalog = mapper.readValue(phonesStrBuilder.toString(), new TypeReference<List<Phone>>() { });

            hPhoneDetails = new Hashtable();
            hPhoneDetailsJSON = new Hashtable();

            for (Phone phone : catalog) {

                String fileDetails = path + phone.getId() + ".json";
                // read the json file
                InputStream inputStreamDetails = classLoader.getResourceAsStream(fileDetails);

                BufferedReader streamReaderDetails = new BufferedReader(new InputStreamReader(inputStreamDetails, "UTF-8"));
                StringBuilder strBuilder = new StringBuilder();

                String inputStr2;
                while ((inputStr2 = streamReaderDetails.readLine()) != null)
                    strBuilder.append(inputStr2);

                ObjectMapper mapper2 = new ObjectMapper();


                PhoneDetails details = mapper2.readValue(strBuilder.toString(), PhoneDetails.class);


                hPhoneDetails.put(phone.getId(),details);
                hPhoneDetailsJSON.put(phone.getId(),strBuilder.toString());




            }

        }

        catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        }
    }


    			
	public List<Phone> getPhones() {
		 return catalog;
	}

    public String getPhonesAsString() {
        return phonesStrBuilder.toString();
    }


    public PhoneDetails getPhonesDetails(String id) {
        return hPhoneDetails.get(id);
    }


    public String getPhonesDetailsAsString(String id) {
        return hPhoneDetailsJSON.get(id);
    }


    public void addComment(Comment comment) {
        comment.setId(commentInc++);
        comments.add(comment);
    }

    public List<Comment> getComment(String phoneId) {
        List<Comment> ret = new ArrayList<Comment>();
        for(Comment c : comments)
        {
            if(c.getPhoneId().equals(phoneId)) ret.add(c);
        }
        return ret;
    }

    public  Comment incLike(int commentId) {

            for(Comment c : comments)
            {
                if(c.getId() == commentId) {
                    c.setLikes(c.getLikes()+1);
                    return c;
                }
            }
            return new Comment();
        }

    public  Comment deleteComment(int commentId) {

        for(Comment c : comments)
        {
            if(c.getId() == commentId) {
                comments.remove(c);
                return c;
            }
        }
        return new Comment();
    }
}

@@ -65,35 +65,8 @@
 .*;%0A
-import java.util.ArrayList;
 %0Aimp
@@ -118,61 +118,8 @@
 t;%0A%0A
-%0Aimport dev.openshift.tapestry.angular.data.Comment;%0A
 impo
@@ -318,18 +318,16 @@
 ence;%0A%0A%0A
-%0A%0A
 public c
@@ -576,45 +576,8 @@
 N;%0A%0A
-    private List%3CComment%3E comments;%0A%0A
     
@@ -657,36 +657,8 @@
 %22;%0A%0A
-    private int commentInc;%0A
 %0A   
@@ -691,96 +691,23 @@
  %7B%0A%0A
-        commentInc = 0;%0A%0A        try %7B%0A%0A            comments = new ArrayList%3CComment%3E();
+%0A        try %7B%0A
 %0A%0A  
@@ -3074,932 +3074,7 @@
 %7D%0A%0A%0A
-    public void addComment(Comment comment) %7B%0A        comment.setId(commentInc++);%0A        comments.add(comment);%0A    %7D%0A%0A    public List%3CComment%3E getComment(String phoneId) %7B%0A        List%3CComment%3E ret = new ArrayList%3CComment%3E();%0A        for(Comment c : comments)%0A        %7B%0A            if(c.getPhoneId().equals(phoneId)) ret.add(c);%0A        %7D%0A        return ret;%0A    %7D%0A%0A    public  Comment incLike(int commentId) %7B%0A%0A            for(Comment c : comments)%0A            %7B%0A                if(c.getId() == commentId) %7B%0A                    c.setLikes(c.getLikes()+1);%0A                    return c;%0A                %7D%0A            %7D%0A            return new Comment();%0A        %7D%0A%0A    public  Comment deleteComment(int commentId) %7B%0A%0A        for(Comment c : comments)%0A        %7B%0A            if(c.getId() == commentId) %7B%0A                comments.remove(c);%0A                return c;%0A            %7D%0A        %7D%0A        return new Comment();%0A    %7D
 %0A%7D%0A

Remove unused implementations