package uk.org.ngo.squeezer.model;

import android.util.Log;

import java.util.HashMap;
import java.util.Map;

import uk.org.ngo.squeezer.itemlist.JiveItemListActivity;
import uk.org.ngo.squeezer.service.HomeMenuHandling;
import uk.org.ngo.squeezer.service.ISqueezeService;

public class CustomJiveItemHandling {

    private static final String TAG = "CustomJiveItemHandling";

    public int ADJUSTED_CUSTOM_SHORTCUT_WEIGHT = 2000;

    final ISqueezeService service;
    final HomeMenuHandling mHomeMenuHandling;
    final JiveItemListActivity mActivity;

    public CustomJiveItemHandling(JiveItemListActivity activity) {
        mActivity = activity;
        service = mActivity.getService();
        mHomeMenuHandling = service.getDelegate().getHomeMenuHandling();
    }

    /**
     * Send long pressed JiveItem
     *
     * @param item
     */
    public boolean triggerCustomShortcut(JiveItem item) {
        item.appendWeight(ADJUSTED_CUSTOM_SHORTCUT_WEIGHT);
        return mHomeMenuHandling.triggerCustomShortcut(item);
    }

    public boolean isCustomShortcut(JiveItem item) {
        return mHomeMenuHandling.customShortcuts.contains(item);
    }

    public boolean isShortcutable(JiveItem item) {
        //  TODO add better check for fitting items
        //  TODO "All titles" is a name that comes up in several occations and will then not be updated
        if ((item.nextWindow != null) || (item.goAction == null)) {
            return false;
        }
        for (String s : item.goAction.action.cmd) {
            if (allowMyMusic(s) || allowApps(s) || allowRadio(s)) {
                return true;
            }
        }
    return false;
    }

    private boolean allowMyMusic(String s) {
        if (s.equals("browselibrary")) {
            ADJUSTED_CUSTOM_SHORTCUT_WEIGHT = 2000;
            return true;
        }
        return false;
    }

    private boolean allowApps(String s) {
        if (s.equals("items")) {
            ADJUSTED_CUSTOM_SHORTCUT_WEIGHT = 2010;
            return true;
        }
        return false;
    }

    private boolean allowRadio(String s) {
        if (s.equals("play")) {
            ADJUSTED_CUSTOM_SHORTCUT_WEIGHT = 2020;
            return true;
        }
        return false;
    }

    public Map<String, Object> convertShortcuts() {
        Map<String, Object> map = new HashMap<>();
        for (JiveItem item : mHomeMenuHandling.customShortcuts) {
            map.put(item.getName(), item.getRecord());
        }
        return map;
    }
}


@@ -33,34 +33,8 @@
 l;%0A%0A
-import android.util.Log;%0A%0A
 impo
@@ -290,203 +290,56 @@
    p
-rivate static final String TAG = %22CustomJiveItemHandling%22;%0A%0A    public int ADJUSTED_CUSTOM_SHORTCUT_WEIGHT = 2000;%0A%0A    final ISqueezeService service;%0A    final HomeMenuHandling mHomeMenuHandling
+ublic int ADJUSTED_CUSTOM_SHORTCUT_WEIGHT = 2000
 ;%0A  
@@ -484,21 +484,65 @@
     
+%7D%0A%0A
     
-service =
+private ISqueezeService service() %7B%0A        return
  mAc
@@ -570,39 +570,83 @@
     
+%7D%0A%0A
     
-mHomeMenuHandling =
+private HomeMenuHandling homeMenuHandling() %7B%0A        return
  service
 .get
@@ -641,16 +641,18 @@
  service
+()
 .getDele
@@ -683,25 +683,24 @@
 ng();%0A    %7D%0A
-%0A
     /**%0A    
@@ -733,34 +733,8 @@
 tem%0A
-     *%0A     * @param item%0A
     
@@ -862,34 +862,33 @@
 %0A        return 
-mH
+h
 omeMenuHandling.
@@ -886,16 +886,18 @@
 Handling
+()
 .trigger
@@ -993,18 +993,17 @@
  return 
-mH
+h
 omeMenuH
@@ -1001,32 +1001,34 @@
 homeMenuHandling
+()
 .customShortcuts
@@ -2276,18 +2276,17 @@
  item : 
-mH
+h
 omeMenuH
@@ -2292,16 +2292,18 @@
 Handling
+()
 .customS

Fix NPE in CustomJiveItemHandling caused by access