package sgc.portal.mobile.web;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import sgc.portal.mobile.model.ManifestIconVo;
import sgc.portal.mobile.model.ManifestShortcutVo;
import sgc.portal.mobile.model.ManifestVo;
import sgc.portal.security.annotation.Secured;
import sgc.portal.security.annotation.SecurityPolicy;

/**
 * @author 5364
 *
 */
@RestController
@RequestMapping("/mobile")
public class MobileController {
    /**
     * @param mv
     * @param request
     * @return
     */
    @RequestMapping({ "/", "/index.html" })
    @Secured(policy = SecurityPolicy.NONE)
    public ModelAndView MobileIndex(ModelAndView mv, HttpServletRequest request) {
        switch (request.getServerName()) {
            case "localhost":
            case "as.sgcetec.co.kr":
            case "mas.sgcetec.co.kr":
            case "as2.sgcetec.co.kr":
            case "mas2.sgcetec.co.kr":
            case "as.sgcenc.co.kr":
            case "mas.sgcenc.co.kr":
            case "as2.sgcenc.co.kr":
            case "mas2.sgcenc.co.kr":
                mv.addObject("title", "SGC As");
                mv.addObject("manifest", "manifest.json");
                break;
            case "portal.sgc.co.kr":
            case "mportal.sgc.co.kr":
            case "portal2.sgc.co.kr":
            case "mportal2.sgc.co.kr":
            default:
                mv.addObject("title", "SGC Portal");
                mv.addObject("manifest", "manifest.json");
                break;
        }
        mv.setViewName("mobile/index");
        return mv;
    }

    /**
     * @param mv
     * @param request
     * @return
     */
    @RequestMapping(value = "/manifest.json")
    @Secured(policy = SecurityPolicy.NONE)
    public Map<String, Object> getManifest(HttpServletRequest request) {
        ObjectMapper mapper = new ObjectMapper();
        ManifestVo manifest = null;
        try {
            InputStream manifestResource = new ClassPathResource("mobile-manifest.json").getInputStream();
            manifest = mapper.readValue(manifestResource, new TypeReference<ManifestVo>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
            manifest = new ManifestVo();
            manifest.setName("SGC Portal");
            manifest.setShort_name("SGC Portal");
            manifest.setIcons(new ArrayList<>());
            manifest.setShortcuts(new ArrayList<>());
            manifest.setStart_url(".");
            manifest.setOrientation("portrait");
            manifest.setDisplay("fullscreen");
            manifest.setBackground_color("#ed1c24");
        }
        manifest.setStart_url("/mobile/");
        List<ManifestShortcutVo> shortcutsNew = new ArrayList<>();
        switch (request.getServerName()) {
            case "localhost":
            case "portal.sgc.co.kr":
            case "mportal.sgc.co.kr":
            case "portal2.sgc.co.kr":
            case "mportal2.sgc.co.kr":
                manifest.setName("SGC Portal");
                manifest.setShort_name("SGC Portal");
                if (manifest.getShortcuts() != null && manifest.getShortcuts().size() > 0) {
                    for (ManifestShortcutVo shortCut : manifest.getShortcuts()) {
                        if (shortCut != null && shortCut.getUrl() != null
                                && (shortCut.getUrl().startsWith("/mobile/#home")
                                        || shortCut.getUrl().startsWith("/mobile/#address"))) {
                            shortcutsNew.add(shortCut);
                        }
                    }
                }
                break;
            case "as.sgcetec.co.kr":
            case "mas.sgcetec.co.kr":
            case "as2.sgcetec.co.kr":
            case "mas2.sgcetec.co.kr":
            case "as.sgcenc.co.kr":
            case "mas.sgcenc.co.kr":
            case "as2.sgcenc.co.kr":
            case "mas2.sgcenc.co.kr":
            default:
                manifest.setName("SGC 협력업체");
                manifest.setShort_name("SGC 협력업체");
                if (manifest.getShortcuts() != null && manifest.getShortcuts().size() > 0) {
                    for (ManifestShortcutVo shortCut : manifest.getShortcuts()) {
                        if (shortCut != null && shortCut.getUrl() != null
                                && (shortCut.getUrl().startsWith("/mobile/#home")
                                        || shortCut.getUrl().startsWith("/mobile/#/as/"))) {
                            shortcutsNew.add(shortCut);
                        }
                    }
                }
                break;
        }
        for (ManifestShortcutVo shortCut : shortcutsNew) {
            if (shortCut.getUrl().startsWith("/#")) {
                shortCut.setUrl("/mobile" + shortCut.getUrl());
            }
            if (shortCut.getShort_name() == null) {
                shortCut.setShort_name(shortCut.getName());
            }
            if (shortCut.getDescription() == null) {
                shortCut.setDescription(shortCut.getName());
            }
        }
        manifest.setShortcuts(shortcutsNew);
        Map<String, Object> manifestMap = new HashMap<>();
        manifestMap.put("name", manifest.getName());
        if (manifest.getShort_name() != null && !"".equals(manifest.getShort_name())) {
            manifestMap.put("short_name", manifest.getShort_name());
        }
        if (manifest.getIcons() != null && manifest.getIcons().size() > 0) {
            manifestMap.put("icons", this.getIcons(manifest.getIcons()));
        }
        if (manifest.getProtocol_handlers() != null && manifest.getProtocol_handlers().size() > 0) {
            manifestMap.put("protocol_handlers", manifest.getProtocol_handlers());
        }
        manifestMap.put("start_url", "./");
        if (manifest.getDisplay() != null && !"".equals(manifest.getDisplay())) {
            manifestMap.put("display", manifest.getDisplay());
        }
        if (manifest.getTheme_color() != null && !"".equals(manifest.getTheme_color())) {
            manifestMap.put("theme_color", manifest.getTheme_color());
        }
        if (manifest.getBackground_color() != null && !"".equals(manifest.getBackground_color())) {
            manifestMap.put("background_color", manifest.getBackground_color());
        }
        if (manifest.getOrientation() != null && !"".equals(manifest.getOrientation())) {
            manifestMap.put("orientation", manifest.getOrientation());
        }
        if (manifest.getShortcuts() != null && manifest.getShortcuts().size() > 0) {
            List<Map<String, Object>> shortcuts = new ArrayList<>();
            for (ManifestShortcutVo item : manifest.getShortcuts()) {
                Map<String, Object> shortcut = new HashMap<>();
                shortcut.put("name", item.getName());
                if (item.getShort_name() != null && !"".equals(item.getShort_name())) {
                    shortcut.put("short_name", item.getShort_name());
                }
                if (item.getDescription() != null && !"".equals(item.getDescription())) {
                    shortcut.put("description", item.getDescription());
                }
                shortcut.put("url", item.getUrl());
                shortcut.put("icons", this.getIcons(item.getIcons()));
                shortcuts.add(shortcut);
            }
            manifestMap.put("shortcuts", shortcuts);
        }
        return manifestMap;
    }

    private List<Map<String, String>> getIcons(List<ManifestIconVo> list) {
        List<Map<String, String>> listMap = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (ManifestIconVo icon : list) {
                String purpose = icon.getPurpose();
                Map<String, String> iconMap = new HashMap<>();
                iconMap.put("sizes", icon.getSizes());
                iconMap.put("src", icon.getSrc());
                iconMap.put("type", icon.getType());
                if (purpose != null && !"".equals(purpose) && !"any".equals(purpose)) {
                    iconMap.put("purpose", purpose);
                }
                listMap.add(iconMap);
            }
        }
        return listMap;
    }

}
