package name.abuchen.portfolio.online.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import name.abuchen.portfolio.model.ClientSettings;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.online.SecuritySearchProvider.ResultItem;
import name.abuchen.portfolio.util.WebAccess;

/* package */ class YahooSymbolSearch
{
    /* package */ static class Result implements ResultItem
    {
        private String symbol;
        private String name;
        private String type;
        private String exchange;

        public static Result from(JSONObject json)
        {
            String symbol = json.get("symbol").toString(); //$NON-NLS-1$
            String name = json.get("name").toString(); //$NON-NLS-1$
            String type = json.get("typeDisp").toString(); //$NON-NLS-1$
            String exchange = json.get("exchDisp").toString(); //$NON-NLS-1$
            return new Result(symbol, name, type, exchange);
        }

        private Result(String symbol, String name, String type, String exchange)
        {
            this.symbol = symbol;
            this.name = name;
            this.type = type;
            this.exchange = exchange;
        }

        /* package */ Result(String name)
        {
            this.name = name;
        }

        @Override
        public String getSymbol()
        {
            return symbol;
        }

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

        @Override
        public String getType()
        {
            return type;
        }

        @Override
        public String getExchange()
        {
            return exchange;
        }

        @Override
        public String getIsin()
        {
            return null;
        }

        @Override
        public String getWkn()
        {
            return null;
        }

        @Override
        public Security create(ClientSettings settings)
        {
            Security security = new Security();
            security.setName(name);
            security.setTickerSymbol(symbol);
            security.setFeed(YahooFinanceQuoteFeed.ID);
            return security;
        }
    }

    public Stream<Result> search(String query) throws IOException
    {
        List<Result> answer = new ArrayList<>();

        @SuppressWarnings("nls")
        String html = new WebAccess("s.yimg.com", "/aq/autoc") //
                        .addParameter("query", query) //
                        .addParameter("region", "DE") //
                        .addParameter("lang", "de-DE") //
                        .addParameter("callback", "YAHOO.util.ScriptNodeDataSource.callbacks") //
                        .get();

        // strip away java script call back method
        int start = html.indexOf('(');
        int end = html.lastIndexOf(')');
        html = html.substring(start + 1, end);

        JSONObject responseData = (JSONObject) JSONValue.parse(html);
        if (responseData != null)
        {
            JSONObject resultSet = (JSONObject) responseData.get("ResultSet"); //$NON-NLS-1$
            if (resultSet != null)
            {
                JSONArray result = (JSONArray) resultSet.get("Result"); //$NON-NLS-1$
                if (result != null)
                {
                    for (int ii = 0; ii < result.size(); ii++)
                        answer.add(Result.from((JSONObject) result.get(ii)));
                }
            }
        }

        return answer.stream();
    }
}

@@ -469,16 +469,81 @@
 bAccess;
+%0Aimport name.abuchen.portfolio.util.WebAccess.WebAccessException;
 %0A%0A/* pac
@@ -2538,24 +2538,50 @@
 yList%3C%3E();%0A%0A
+        try%0A        %7B%0A    
         @Sup
@@ -2601,16 +2601,20 @@
 (%22nls%22)%0A
+    
         
@@ -2687,32 +2687,36 @@
                 
+    
 .addParameter(%22q
@@ -2748,32 +2748,36 @@
                 
+  
+  
 .addParameter(%22r
@@ -2785,32 +2785,36 @@
 gion%22, %22DE%22) //%0A
+    
                 
@@ -2871,32 +2871,36 @@
                 
+    
 .addParameter(%22c
@@ -2981,16 +2981,20 @@
         
+  
+  
 .get();%0A
@@ -2990,24 +2990,29 @@
    .get();%0A%0A
+%0A    
         // s
@@ -3054,24 +3054,28 @@
 hod%0A        
+    
 int start = 
@@ -3097,24 +3097,28 @@
 ');%0A        
+ 
+   
 int end = ht
@@ -3146,16 +3146,20 @@
         
+   
+ 
 html = h
@@ -3186,24 +3186,28 @@
 + 1, end);%0A%0A
+    
         JSON
@@ -3268,24 +3268,28 @@
 l);%0A        
+ 
+   
 if (response
@@ -3302,34 +3302,42 @@
 = null)%0A        
-%7B%0A
+    %7B%0A    
             JSON
@@ -3417,32 +3417,36 @@
 -1$%0A            
+    
 if (resultSet !=
@@ -3456,34 +3456,42 @@
 ll)%0A            
-%7B%0A
+    %7B%0A    
                 
@@ -3568,32 +3568,36 @@
                 
+    
 if (result != nu
@@ -3608,34 +3608,42 @@
                 
-%7B%0A
+    %7B%0A    
                 
@@ -3713,16 +3713,20 @@
         
+    
 answer.a
@@ -3779,32 +3779,36 @@
                 
+    
 %7D%0A            %7D%0A
@@ -3797,33 +3797,108 @@
   %7D%0A            
-%7D
+    %7D%0A            %7D%0A        %7D%0A        catch(WebAccessException ex)%0A        %7B
 %0A        %7D%0A%0A    

Allow searching securities when Yahoo symbol search fails