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.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

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

    @Autowired
    private CommunityDAO communityDAO;

    @Autowired
    private LayoutDAO layoutDAO;

    @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);
            Layout layout = layoutDAO.findById(community.getLayout());
            communityResponse.setLayout(layout);
            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);
        Layout layout = layoutDAO.findById(community.getLayout());
        communityResponse.setLayout(layout);
//        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());

        Statistics statistics = statisticsDAO.findByPid(com.getPid());
        statistics.setPid(community.getPid());
        statisticsDAO.save(statistics);

        CommunitySubscribers communitySubscribers =  communitySubscribersDAO.findByPid(com.getPid());
        communitySubscribers.setPid(community.getPid());
        communitySubscribersDAO.save(communitySubscribers);

        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);
            if(page.getRoute().equals("/curators") || page.getRoute().equals("/organizations")) {
                communityPage.setIsEnabled(false);
            } else {
                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();

        divHelpContentController.addDivHelpContentsInCommunity(savedCommunity.getPid(), id, null);
        pageHelpContentController.addPageHelpContentsInCommunity(savedCommunity.getPid(), id);
        /*
        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);
        Layout layout = communityResponse.getLayout();
        community.setLayout(layout.getId());

        return community;
    }

    @RequestMapping(value = "/community/delete", method = RequestMethod.POST)
    public Boolean deleteCommunities(@RequestBody List<String> communities) throws Exception {
        for (String id: communities) {
            Community community = communityDAO.findById(id);
            String pid = community.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());
            }

            Layout layout = layoutDAO.findById(community.getLayout());
            if(layout != null) {
                layoutDAO.delete(layout.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());
                    Iterator<DivId> divIdIterator = divIds.iterator();

                    while (divIdIterator.hasNext()) {
                        DivId divId = divIdIterator.next();
                        if((pid.equals("openaire") && !divId.getOpenaire()) ||
                                (pid.equals("connect") && !divId.getConnect()) ||
                                (!pid.equals("openaire") && !pid.equals("connect") && !divId.getCommunities())) {
                            divIdIterator.remove();
                        }
                    }

                    if(divIds.isEmpty()) {
                        continue;
                    }
                }

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

                if((pid.equals("openaire") && p.getOpenaire()) || (pid.equals("connect") && p.getConnect()) ||(!pid.equals("openaire") && !pid.equals("connect") && p.getCommunities())) {
                    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;
    }

    @RequestMapping(value = "/community/{pid}/layout", method = RequestMethod.GET)
    public Layout getLayoutForCommunity(@PathVariable(value = "pid") String pid) {
        Community community = communityDAO.findByPid(pid);
        if(community.getLayout() != null) {
            return layoutDAO.findById(community.getLayout());
        } else {
            return null;
        }
    }

    @RequestMapping(value = "/community/{pid}/layout", method = RequestMethod.POST)
    public Layout updateLayoutForCommunity(@PathVariable(value = "pid") String pid, @RequestBody Layout layout) {
        Community community = communityDAO.findByPid(pid);
        if(community.getLayout() != null) {
            layout.setId(community.getLayout());
            layout = layoutDAO.save(layout);
        } else {
            layout = layoutDAO.save(layout);
            community.setLayout(layout.getId());
            communityDAO.save(community);
        }
        return layout;
    }


    @RequestMapping(value = "/community/{pid}/pagehelpcontent", method = RequestMethod.GET)
    public Map<String, List<PageHelpContentResponse>> getPageHelpContentsByPosition(@PathVariable(value = "pid") String pid,
                                                                       @RequestParam(required=false) String page,
                                                                       @RequestParam(required=false) String active) {
        Map<String, List<PageHelpContentResponse>> pageHelpContentResponses = new HashMap<>();

        List<PageHelpContentResponse> pageHelpContents = null;
        pageHelpContents = pageHelpContentController.getPageHelpContents(pid, page, null, active, null);

        pageHelpContentResponses.put("top", new ArrayList<>());
        pageHelpContentResponses.put("bottom", new ArrayList<>());
        pageHelpContentResponses.put("left", new ArrayList<>());
        pageHelpContentResponses.put("right", new ArrayList<>());

        for (PageHelpContentResponse pageHelpContentResponse : pageHelpContents) {
            pageHelpContentResponses.get(pageHelpContentResponse.getPlacement()).add(pageHelpContentResponse);
        }


        return pageHelpContentResponses;
    }

    @RequestMapping(value = "/community/{pid}/divhelpcontent", method = RequestMethod.GET)
    public Map<String, List<DivHelpContentResponse>> getDivHelpContentsByPosition(@PathVariable(value = "pid") String pid,
                                                                                    @RequestParam(required=false) String page,
                                                                                    @RequestParam(required=false) String active) {
        Map<String, List<DivHelpContentResponse>> divHelpContentResponses = new HashMap<>();

        List<DivHelpContentResponse> divHelpContents = null;
        divHelpContents = divHelpContentController.getDivHelpContents(pid, page, null, active);

        for (DivHelpContentResponse divHelpContentResponse : divHelpContents) {
            if(!divHelpContentResponses.containsKey(divHelpContentResponse.getDivId())) {
                divHelpContentResponses.put(divHelpContentResponse.getDivId().getName(), new ArrayList<>());
            }
            divHelpContentResponses.get(divHelpContentResponse.getDivId().getName()).add(divHelpContentResponse);
        }


        return divHelpContentResponses;
    }
}

