package com.enow.daos.redisDAO;

/**
 * Created by writtic on 2016. 9. 12..
 */

import java.util.*;

import com.enow.persistence.dto.NodeDTO;
import com.enow.persistence.redis.RedisDB;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import redis.clients.jedis.Jedis;


public class NodeDAO implements INodeDAO {

    private static final String NODE_PREFIX = "node-";

    @Override
    public String toID(String roadMapID, String mapID) {
        String id = roadMapID + "-" + mapID;
        return id;
    }

    @Override
    public NodeDTO jsonObjectToNode(JSONObject jsonObject){

        String roadMapID = (String) jsonObject.get("roadMapId");
        String mapID = (String) jsonObject.get("mapId");
        String topic = (String) jsonObject.get("topic");
        JSONArray outingJSON = (JSONArray) jsonObject.get("outingNode");
        Integer refer = 0;
        if(outingJSON != null){
            refer = outingJSON.size();
        }
        JSONObject payload = (JSONObject) jsonObject.get("payload");
        NodeDTO dto = new NodeDTO(roadMapID, mapID, topic, payload.toJSONString(), "" + refer);
        return dto;
    }

    @Override
    public String addNode(NodeDTO dto) {
        Jedis jedis = RedisDB.getConnection();
        String id = dto.getRoadMapID() + "-" + dto.getMapID();

        Set<String> keys = jedis.keys("node-*");
        Iterator<String> iter = keys.iterator();
        ArrayList<String> ids = new ArrayList<>();

        boolean nodeExists = false;

        while(iter.hasNext()) {
            String key = iter.next();
            key = key.substring(5, key.length());
            ids.add(key);
            if(key.equals(id)) {
                nodeExists = true;
            }
        }
        if(!nodeExists) {
            System.out.println("refer: " + dto.getRefer());
            jedis.lpush(NODE_PREFIX + id, dto.getTopic());
            jedis.lpush(NODE_PREFIX + id, dto.getPayload());
            jedis.lpush(NODE_PREFIX + id, dto.getRefer());
            return id;
        } else {
            jedis.del(NODE_PREFIX + id);
            jedis.lpush(NODE_PREFIX + id, dto.getTopic());
            jedis.lpush(NODE_PREFIX + id, dto.getPayload());
            jedis.lpush(NODE_PREFIX + id, dto.getRefer());
            return id + " overwritten";
        }
    }
    @Override
    public NodeDTO getNode(String ID) {
        Jedis jedis = RedisDB.getConnection();
        StringTokenizer tokenizer = new StringTokenizer(ID, "-");
        String roadMapID = tokenizer.nextToken();
        String mapID = tokenizer.nextToken();
        String id = roadMapID + "-" + mapID;
        List<String> result = jedis.lrange(NODE_PREFIX + id, 0, -1);
        if (result.size() > 1) {
            System.out.println("result.get(0) : " + result.get(0));
            System.out.println("result.get(1) : " + result.get(1));
            System.out.println("result.get(2) : " + result.get(2));
            NodeDTO dto = new NodeDTO(roadMapID, mapID, result.get(2), result.get(1), result.get(0));
            return dto;
        } else {
            return null;
        }
    }

    @Override
    public List<NodeDTO> getAllNodes() {
        Jedis jedis = RedisDB.getConnection();
        List<NodeDTO> nodes = new ArrayList<>();
        Set<String> keys = jedis.keys("node-*");
        for (String key : keys) {
            key = key.substring(5, key.length());
            nodes.add(getNode(key));
        }
        return nodes;
    }

    @Override
    public void updateNode(NodeDTO dto) {
        Jedis jedis = RedisDB.getConnection();
        String id = dto.getRoadMapID() + "-" + dto.getMapID();
        jedis.rpop(NODE_PREFIX + id);
        jedis.rpush(NODE_PREFIX + id, dto.getPayload());
    }

    @Override
    public void updateRefer(NodeDTO dto) {
        Jedis jedis = RedisDB.getConnection();
        String id = dto.getRoadMapID() + "-" + dto.getMapID();
        Integer temp = Integer.parseInt(dto.getRefer());
        --temp;
        if (temp > 0) {
            jedis.rpop(NODE_PREFIX + id);
            jedis.rpop(NODE_PREFIX + id);
            jedis.rpush(NODE_PREFIX + id, dto.getPayload());
            jedis.rpush(NODE_PREFIX + id, "" + temp);
        } else {
            jedis.del(NODE_PREFIX + id);
        }
    }

    @Override
    public void deleteAllNodes() {
        Jedis jedis = RedisDB.getConnection();
        Set<String> keys = jedis.keys("node-*");
        Iterator<String> iter = keys.iterator();
        while (iter.hasNext()) {
            jedis.del(iter.next());
        }
    }
    @Override
    public void deleteNode(String ID) {
        Jedis jedis = RedisDB.getConnection();
        jedis.del(NODE_PREFIX + ID);
    }
}

@@ -3656,33 +3656,33 @@
 ;%0A        jedis.
-r
+l
 pop(NODE_PREFIX 
@@ -3694,33 +3694,71 @@
 ;%0A        jedis.
-r
+lpop(NODE_PREFIX + id);%0A        jedis.l
 push(NODE_PREFIX
@@ -3779,24 +3779,79 @@
 Payload());%0A
+        jedis.lpush(NODE_PREFIX + id, dto.getRefer());%0A
     %7D%0A%0A    @
@@ -4028,20 +4028,21 @@
 Integer 
-temp
+refer
  = Integ
@@ -4080,20 +4080,21 @@
       --
-temp
+refer
 ;%0A      
@@ -4099,20 +4099,21 @@
     if (
-temp
+refer
  %3E 0) %7B%0A
@@ -4134,154 +4134,51 @@
 dis.
-rpop(NODE_PREFIX + id);%0A            jedis.rpop(NODE_PREFIX + id);%0A            jedis.rpush(NODE_PREFIX + id, dto.getPayload());%0A            jedis.r
+lpop(NODE_PREFIX + id);%0A            jedis.l
 push
@@ -4205,12 +4205,13 @@
 %22 + 
-temp
+refer
 );%0A 

Change rpush to lpush on Redis DAO