package eu.dnetlib.uoaadmintools.controllers;

import eu.dnetlib.uoaadmintools.dao.*;
import eu.dnetlib.uoaadmintools.entities.*;

import eu.dnetlib.uoaadmintools.entities.statistics.Statistics;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.swing.text.html.HTML;
import java.util.*;

@RestController
@CrossOrigin(origins = "*")
public class CommunityController {
    private final Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private CommunityDAO communityDAO;

    @Autowired
    private PageDAO pageDAO;

    @Autowired
    private EntityDAO entityDAO;

    @Autowired
    private DivIdDAO divIdDAO;

    @Autowired
    private PageHelpContentController pageHelpContentController;

    @Autowired
    private DivHelpContentController divHelpContentController;

    @Autowired
    private HtmlPageContentController htmlPageContentController;

    @Autowired
    private DivIdController divIdController;

    @Autowired
    private StatisticsDAO statisticsDAO;
    @Autowired
    private CommunitySubscribersDAO  communitySubscribersDAO;

    @RequestMapping(value = "/community", method = RequestMethod.GET)
    public List<Community> getAllCommunities() {
        List<Community> communities = communityDAO.findAll();

        return communities;
    }

    @RequestMapping(value = "/communityFull", method = RequestMethod.GET)
    public List<CommunityResponse> getAllCommunitiesFull() {
        List<Community> communities = communityDAO.findAll();
        List<CommunityResponse> communitiesResponse = new ArrayList<>();
        for(Community community : communities) {
            CommunityResponse communityResponse = new CommunityResponse(community);

            List<CommunityPage> pages = this.getPagesForCommunityByType(community.getPid(), null, null, null);
            log.debug("PAGES number="+pages.size());
            Iterator<CommunityPage> iteratorPages = pages.iterator();
            while(iteratorPages.hasNext()) {
                CommunityPage page = iteratorPages.next();
                if(!page.getIsEnabled()) {
                    iteratorPages.remove();
                }
            }
            communityResponse.setPages(pages);
            log.debug("PAGES set");

            List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getPid(), null);
            log.debug("ENTITIES number="+entities.size());
            Iterator<CommunityEntity> iteratorEntities = entities.iterator();
            while(iteratorEntities.hasNext()) {
                CommunityEntity entity = iteratorEntities.next();
                if(!entity.getIsEnabled()) {
                    iteratorEntities.remove();
                }
            }
            communityResponse.setEntities(entities);

            communitiesResponse.add(communityResponse);
        }
        return communitiesResponse;
    }

    @RequestMapping(value = "/communityFull/{pid}", method = RequestMethod.GET)
    public CommunityResponse getCommunityFull(@PathVariable(value = "pid") String pid) {
        Community community = communityDAO.findByPid(pid);
        CommunityResponse communityResponse = new CommunityResponse(community);

        List<CommunityPage> pages = this.getPagesForCommunityByType(community.getPid(), null, null, null);
        Iterator<CommunityPage> iteratorPages = pages.iterator();
        while(iteratorPages.hasNext()) {
            CommunityPage page = iteratorPages.next();
            if(!page.getIsEnabled()) {
                iteratorPages.remove();
            }
        }
        communityResponse.setPages(pages);

        List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getPid(), null);
        Iterator<CommunityEntity> iteratorEntities = entities.iterator();
        while(iteratorEntities.hasNext()) {
            CommunityEntity entity = iteratorEntities.next();
            if(!entity.getIsEnabled()) {
                iteratorEntities.remove();
            }
        }
        communityResponse.setEntities(entities);
//        communityResponse.setPages(this.getPagesForCommunityByType(community.getId(), null));
//        communityResponse.setEntities(this.getEntitiesForCommunity(community.getId()));

        return communityResponse;
    }
/*

    @RequestMapping(value = "/communityFullByName/{name}", method = RequestMethod.GET)
    public CommunityResponse getCommunityFullByName(@PathVariable(value = "name") String name) {
        Community community = communityDAO.findByName(name);
        CommunityResponse communityResponse = new CommunityResponse(community);

        List<CommunityPage> pages = this.getPagesForCommunityByType(community.getId(), null, null);
        Iterator<CommunityPage> iteratorPages = pages.iterator();
        while(iteratorPages.hasNext()) {
            CommunityPage page = iteratorPages.next();
            if(!page.getIsEnabled()) {
                iteratorPages.remove();
            }
        }
        communityResponse.setPages(pages);

        List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getId(), null);
        Iterator<CommunityEntity> iteratorEntities = entities.iterator();
        while(iteratorEntities.hasNext()) {
            CommunityEntity entity = iteratorEntities.next();
            if(!entity.getIsEnabled()) {
                iteratorEntities.remove();
            }
        }
        communityResponse.setEntities(entities);

        return communityResponse;
    }
*/

    @RequestMapping(value = "/community/update", method = RequestMethod.POST)
    public CommunityResponse updateCommunity(@RequestBody Community community) {
        Community com = communityDAO.findById(community.getId());
        com.setName(community.getName());
        com.setPid(community.getPid());
        // = this.getCommunityByCommunityResponse(communityResponse);
        communityDAO.save(com);
        CommunityResponse communityResponse = this.getCommunityFull(community.getPid());

        return communityResponse;
    }

    @RequestMapping(value = "/community/save", method = RequestMethod.POST)
    public CommunityResponse insertCommunity(@RequestBody Community community) {
        //Community community = this.getCommunityByCommunityResponse(communityResponse);

        List<CommunityEntity> communityEntities = new ArrayList<>();
        List<CommunityPage> communityPages = new ArrayList<>();
        Map<String, Boolean> entities = new HashMap<>();
        Map<String, Boolean> pages = new HashMap<>();

        for(Entity entity : entityDAO.findAll()) {
            entities.put(entity.getId(), true);

            CommunityEntity communityEntity = new CommunityEntity(entity);
            communityEntity.setIsEnabled(true);
            communityEntities.add(communityEntity);
        }

        for(Page page : pageDAO.findAll()) {
            pages.put(page.getId(), true);

            CommunityPage communityPage = new CommunityPage(page);
            communityPage.setIsEnabled(true);

            communityPages.add(communityPage);
        }

        community.setEntities(entities);
        community.setPages(pages);
        Statistics statistics =  new Statistics(community.getPid());
        statisticsDAO.save(statistics);
        CommunitySubscribers communitySubscribers =  new CommunitySubscribers(community.getPid());
        communitySubscribersDAO.save(communitySubscribers);
        Community savedCommunity = communityDAO.save(community);
        CommunityResponse communityResponse = this.getCommunityFull(savedCommunity.getPid());

        log.debug("pid of saved community: "+savedCommunity.getPid());

        String id = savedCommunity.getId();

        String link_context_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Select a research community and/or a category and search for a community concept, or browse to the community tree through the categories</div> </div>";
        String link_project_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Search for projects using project name or grant id. Limit results filtering by funder.</div> </div>";
        String link_result_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span></div> Search for research results in OpenAIRE information space, Datacite, CrossRef or ORCID. <div class=\"uk-text-small\">Use keywords, DOI (more than one - space separated), author&#39;s ORCID</div> </div>";
        String link_result_bulk_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Upload a csv file containing a list of DOIs. For each DOI found in the file, metadata will be fetched from CrossRef or Datacite and will be added to your selected research results.</div> <div class=\"uk-margin-top uk-text-small\"><span class=\"uk-text-bold\">CSV format:</span> <ul class=\"uk-list\"> <li>The format of CSV file should be &quot;DOI&quot;,&quot;ACCESS_MODE&quot;,&quot;DATE&quot;.</li> <li>The value &quot;DOI&quot; is required</li> <li>Access mode column should have values: &quot;OPEN&quot;,&quot;CLOSED&quot; or &quot;EMBARGO&quot;.</li> <li>Date column valid format is YYYY-MM-DD and is required when access mode has value EMBARGO.</li> <li>In case access mode is not available default value is &quot;OPEN&quot;.</li> </ul> </div> </div>";
        String link_metadata_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Manage access mode &amp; type of selected research results. For OpenAIRE this functionality isn&#39;t available.</div> </div>";

        DivId div = null;
        div=divIdDAO.findByName("link-context-form");
        if(div != null) {
            String link_context_form = div.getId();
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_context_form, id, link_context_form_content, false));
        }

        div=divIdDAO.findByName("link-project-form");
        if(div != null) {
            String link_project_form = div.getId();
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_project_form, id, link_project_form_content, false));
        }

        div=divIdDAO.findByName("link-result-form");
        if(div != null) {
            String link_result_form = div.getId();
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_result_form, id, link_result_form_content, true));
        }

        div=divIdDAO.findByName("link-result-bulk");
        if(div != null) {
            String link_result_bulk = div.getId();
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_result_bulk, id, link_result_bulk_content, true));
        }

        div=divIdDAO.findByName("link-metadata");
        if(div != null) {
            String link_metadata = div.getId();
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_metadata, id, link_metadata_content, false));
        }

        Page page = null;
        page = pageDAO.findByRoute("/about" );
        if(page != null) {
            String htmlContent = "<div><div class=\"uk-article-title custom-article-title\"> About the community </div> <p> This is an introductory text. To be updated... </p> </div>";
            HtmlPageContent htmlPageContent = new HtmlPageContent(page.getId(), id, htmlContent);
            htmlPageContentController.updateHtmlPageContent(htmlPageContent);
        }

        page = pageDAO.findByRoute("/organizations");
        if(page != null) {
            String htmlContent = "<div><div class=\"uk-article-title custom-article-title\"> Organizations related to the community </div> <p> This is an introductory text. Here follows the list of organizations... </p> <div class=\"uk-child-width-1-3@m uk-text-center uk-grid-match \" uk-grid > <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"https://upload.wikimedia.org/wikipedia/el/2/2b/Logo_uoa_blue.png\" alt=\"\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"https://www.uoa.gr/\">University of Athens</a> </h3> </div> </div> <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"https://pbs.twimg.com/profile_images/631127495933165569/ElbqhHK0_400x400.jpg\" alt=\"\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"https://www.athena-innovation.gr/en\">Athena Research & Innovation center</a> </h3> </div> </div> <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"\" alt=\"Logo 1\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"\">Organization 1</a> </h3> </div> </div> <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"\" alt=\"Logo 2\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"\">Organization 2</a> </h3> </div> </div> <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"\" alt=\"Logo 3\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"\">Organization 3</a> </h3> </div> </div> </div></div>";
            HtmlPageContent htmlPageContent = new HtmlPageContent(page.getId(), id, htmlContent);
            htmlPageContentController.updateHtmlPageContent(htmlPageContent);
        }

        return communityResponse;
    }

    private Community getCommunityByCommunityResponse(CommunityResponse communityResponse) {
        Community community = new Community();
        community.setId(communityResponse.getId());
        community.setName(communityResponse.getName());

        List<CommunityEntity> fullEntities = communityResponse.getEntities();
        Map<String, Boolean> entities = new HashMap<String, Boolean>();
        for(CommunityEntity entity : fullEntities) {
            entities.put(entity.getId(), true);
        }
        for(Entity entity : entityDAO.findAll()) {
            if(!entities.containsKey(entity.getId())) {
                entities.put(entity.getId(), false);
            }
        }
        community.setEntities(entities);

        List<CommunityPage> fullPages = communityResponse.getPages();
        Map<String, Boolean> pages = new HashMap<String, Boolean>();
        for(CommunityPage page : fullPages) {
            pages.put(page.getId(), true);
        }
        for(Page page : pageDAO.findAll()) {
            if(!pages.containsKey(page.getId())) {
                pages.put(page.getId(), false);
            }
        }
        community.setPages(pages);

        return community;
    }

    @RequestMapping(value = "/community/delete", method = RequestMethod.POST)
    public Boolean deleteCommunities(@RequestBody List<String> communities) throws Exception {
        for (String id: communities) {
            String pid = communityDAO.findById(id).getPid();

            // delete div contents related to this community
            List<DivHelpContentResponse> divHelpContentResponses = divHelpContentController.getDivHelpContents(pid, null, null, null);
            for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
                divHelpContentController.deleteDivHelpContent(divHelpContentResponse.getId());
            }

            // delete page contents related to this community
            List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentController.getPageHelpContents(pid, null, null, null, null);
            for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
                pageHelpContentController.deletePageHelpContent(pageHelpContentResponse.getId());
            }

            List<HtmlPageContent> htmlPageContents = htmlPageContentController.getHtmlPageContents(pid, null);
            for(HtmlPageContent htmlPageContent : htmlPageContents) {
                htmlPageContentController.deleteHtmlPageContent(htmlPageContent.getId());
            }

            Statistics stats = statisticsDAO.findByPid(pid);
            if(stats != null) {
                statisticsDAO.delete(stats.getId());
            }

            CommunitySubscribers communitySubscribers = communitySubscribersDAO.findByPid(pid);
            if(communitySubscribers != null) {
                communitySubscribersDAO.delete(communitySubscribers.getId());
            }

            communityDAO.delete(id);
        }

        return true;
    }

//    @RequestMapping(value = "/community", method = RequestMethod.DELETE)
//    public void deleteAllCommunities() {
//        communityDAO.deleteAll();
//    }

    @RequestMapping(value = "/community", method = RequestMethod.POST)
    public Community insertOrUpdateCommunity(@RequestBody Community community) {
        return communityDAO.save(community);
    }

    @RequestMapping(value = "/community/{pid}", method = RequestMethod.GET)
    public Community getCommunity(@PathVariable(value = "pid") String pid) {
        log.debug("PID: "+ pid);
        return communityDAO.findByPid(pid);
    }

    @RequestMapping(value = "/community/{id}", method = RequestMethod.DELETE)
    public void deleteCommunity(@PathVariable(value = "id") String id) {
        communityDAO.delete(id);
    }

    @RequestMapping(value = "/community/{pid}/pages", method = RequestMethod.GET)
    public List<CommunityPage> getPagesForCommunityByType(@PathVariable(value = "pid") String pid,
                                                          @RequestParam(value="page_type", required=false) String page_type,
                                                          @RequestParam(value="page_route", required=false) String page_route,
                                                          @RequestParam(value="div", required = false) String div) {
        List<CommunityPage> return_pages = new ArrayList<CommunityPage>();
        Map<String, Boolean> pages = communityDAO.findByPid(pid).getPages();

        if(pages != null) {
            for (Map.Entry<String, Boolean> page : pages.entrySet()) {
                if(div != null && div.equals("true")) {
                    Community community = communityDAO.findByPid(pid);
                    List<DivId> divIds = divIdDAO.findByPagesContaining(page.getKey());
                    if(divIds.isEmpty()) {
                        continue;
                    }
                }

                Page p = pageDAO.findById(page.getKey());

                if((pid.equals("openaire") && p.getOpenaire()) ||(!pid.equals("openaire") && p.getConnect())) {
                    if ((page_type == null && page_route == null) || (page_route == null && p.getType().equals(page_type))
                            || p.getRoute().equals(page_route)) {
                        CommunityPage communityPage = new CommunityPage(p);

                        List<Entity> entities = new ArrayList<>();
                        for (String entity : p.getEntities()) {
                            entities.add(entityDAO.findById(entity));
                        }
                        communityPage.setEntities(entities);
                        communityPage.setIsEnabled(page.getValue());

                        return_pages.add(communityPage);

                        if (page_route != null) {
                            break;
                        }
                    }
                }
            }
        }
        return_pages.sort(Comparator.comparing(CommunityPage::getName));
        return return_pages;
    }

    @RequestMapping(value = "/community/{id}/page", method = RequestMethod.POST)
    public Community insertOrUpdatePage(@PathVariable(value = "id") String id, @RequestBody CommunityPage page) {
        Community community = communityDAO.findById(id);
        Map<String, Boolean> pages = community.getPages();

        String name = page.getName();
        boolean isEnabled = page.getIsEnabled();

        pages.put(name, isEnabled);
        community.setPages(pages);

        return communityDAO.save(community);
    }

    @RequestMapping(value = "community/{pid}/page/toggle", method = RequestMethod.POST)
    public Community togglePage(@PathVariable(value = "pid") String pid, @RequestBody List<String> pageIds, @RequestParam String status) throws Exception {
        Community community = communityDAO.findByPid(pid);
        Map<String, Boolean> pages = community.getPages();

        for (String pageId: pageIds) {
            log.debug("Toggle community page: " + pageId + " of community: " + pid + " to " + status);
            pages.put(pageId, Boolean.parseBoolean(status));
        }

        community.setPages(pages);
        return communityDAO.save(community);
    }

    @RequestMapping(value = "community/{pid}/entity/toggle", method = RequestMethod.POST)
    public Community toggleEntity(@PathVariable(value = "pid") String pid, @RequestBody List<String> entityIds, @RequestParam String status) throws Exception {
        Community community = communityDAO.findByPid(pid);
        Map<String, Boolean> entities = community.getEntities();
        Map<String, Boolean> pages = community.getPages();

        for (String entityId: entityIds) {
            log.debug("Toggle community entity: " + entityId + " of community: " + pid + " to " + status);

            entities.put(entityId, Boolean.parseBoolean(status));

            if(pages != null) {
                for (Map.Entry<String, Boolean> pageEntry : pages.entrySet()) {
                    Page page = pageDAO.findById(pageEntry.getKey());
                    if (page.getEntities().contains(entityId) && page.getType().equals("search")) {
                        pages.put(pageEntry.getKey(), Boolean.parseBoolean(status));
                    }
                }
            }
        }

        community.setEntities(entities);
        return communityDAO.save(community);
    }

    @RequestMapping(value = "/community/{pid}/entities", method = RequestMethod.GET)
    public List<CommunityEntity> getEntitiesForCommunity(@PathVariable(value = "pid") String pid, @RequestParam(value="entity", required=false) String entity) {
        List<CommunityEntity> return_entities = new ArrayList<CommunityEntity>();
        Map<String, Boolean> entities = communityDAO.findByPid(pid).getEntities();

        log.debug("/community/"+pid+"/entities -- entity: "+entity);
        if (entity != null) {
            String entityId = entityDAO.findByPid(entity).getId();
            CommunityEntity communityEntity = new CommunityEntity(entityDAO.findById(entityId));
            communityEntity.setIsEnabled(entities.get(entityId));
            return_entities.add(communityEntity);
        } else {
            if(entities != null) {
                for (Map.Entry<String, Boolean> _entity : entities.entrySet()) {
                    CommunityEntity communityEntity = new CommunityEntity(entityDAO.findById(_entity.getKey()));
                    communityEntity.setIsEnabled(_entity.getValue());
                    return_entities.add(communityEntity);
                }
            }
        }
        return return_entities;
    }
}

