package com.dse.se.dao.cql;

import com.datastax.driver.core.*;
import com.dse.se.dao.ISketchDAO;
import com.dse.se.dto.Approximate;
import com.dse.se.dto.TopHashTagsDTO;
import com.dse.se.dto.UniqueUsersDTO;
import com.google.common.base.Stopwatch;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Component
public class SketchDAO implements ISketchDAO {

    //yyyy-mm-dd HH:mm:ssZ
    private static final SimpleDateFormat resultformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat dateformat = new SimpleDateFormat("MM-dd-YYYY");



    public static void main (String[] args) throws ParseException{
        new SketchDAO().getUniqueUsersForToday();
    }

    @Override
    public UniqueUsersDTO getUniqueUsersForToday() throws ParseException {

        Session session = DataStaxSessionFactory.getInstance().getSession();

        String today = dateformat.format(new Date());


        PreparedStatement preparedUniqueUsersForToday = DataStaxSessionFactory.getInstance().getPreparedUniqueUsersForToday();

        BoundStatement bound = preparedUniqueUsersForToday.bind("tweets",  "03-08-2018");

        Stopwatch stopwatch = Stopwatch.createStarted();
        ResultSet results = session.execute(bound);
        stopwatch.stop();

        List<Approximate> approximateList = new ArrayList<>(results.getAvailableWithoutFetching());

        //TODO Use Object Mapper
        for(Row oneResult : results){

            Date time = oneResult.getTimestamp(0);
            Integer uniqueUsers = oneResult.getInt(1);

            Approximate oneApproximate = new Approximate();
            oneApproximate.setTime(resultformat.format(time));
            oneApproximate.setUniqueUsers(uniqueUsers);

            approximateList.add(oneApproximate);
        }

        UniqueUsersDTO uniqueUsersDTO = new UniqueUsersDTO();
        uniqueUsersDTO.setApproximates(approximateList.toArray(new Approximate[approximateList.size()]));
        uniqueUsersDTO.setResultTime(stopwatch.elapsed(TimeUnit.MILLISECONDS));

        return uniqueUsersDTO;
    }
    @Override
    public UniqueUsersDTO getUniqueUsersRollup() throws ParseException {

        Session session = DataStaxSessionFactory.getInstance().getSession();

        String today = dateformat.format(new Date());


        PreparedStatement preparedUniqueUsersRollup = DataStaxSessionFactory.getInstance().getPreparedUniqueUsersRollup();


        BoundStatement bound = preparedUniqueUsersRollup.bind("tweets",   "03-08-2018");

        Stopwatch stopwatch = Stopwatch.createStarted();
        ResultSet results = session.execute(bound);
        stopwatch.stop();

        List<Approximate> approximateList = new ArrayList<>(results.getAvailableWithoutFetching());

        //TODO Use Object Mapper
        for(Row oneResult : results){

            Date time = oneResult.getTimestamp(0);
            Integer uniqueUsers = oneResult.getInt(1);

            Approximate oneApproximate = new Approximate();
            oneApproximate.setTime(resultformat.format(time));
            oneApproximate.setUniqueUsers(uniqueUsers);

            approximateList.add(oneApproximate);
        }

        UniqueUsersDTO uniqueUsersDTO = new UniqueUsersDTO();
        uniqueUsersDTO.setApproximates(approximateList.toArray(new Approximate[approximateList.size()]));
        uniqueUsersDTO.setResultTime(stopwatch.elapsed(TimeUnit.MILLISECONDS));

        return uniqueUsersDTO;
    }
    @Override
    public TopHashTagsDTO getTopTweetsRollup() throws ParseException{

        Session session = DataStaxSessionFactory.getInstance().getSession();

        String today = dateformat.format(new Date());


        PreparedStatement preparedTopTweetsRollup = DataStaxSessionFactory.getInstance().getPreparedTopTweetsRollup();

        BoundStatement bound = preparedTopTweetsRollup.bind("tweets",  "03-08-2018");

        Stopwatch stopwatch = Stopwatch.createStarted();
        ResultSet results = session.execute(bound);
        stopwatch.stop();


        Integer count  = results.getAvailableWithoutFetching();

        TopHashTagsDTO topHashTagsDTO = new TopHashTagsDTO();
        topHashTagsDTO.setResultTime(stopwatch.elapsed(TimeUnit.MILLISECONDS));

        if(count > 0){
            Row oneResult = results.one();

            Date time = oneResult.getTimestamp(0);

            Map<String, Long> map = oneResult.getMap(1, String.class, Long.class);

            topHashTagsDTO.setTopTags(map);
        }


        return topHashTagsDTO;
    }
}
@@ -1411,36 +1411,29 @@
 (%22tweets%22,  
-%2203-08-2018%22
+today
 );%0A%0A        
@@ -2786,29 +2786,21 @@
 eets%22,  
- %2203-08-2018%22
+today
 );%0A%0A    
@@ -4150,21 +4150,13 @@
 s%22, 
- %2203-08-2018%22
+today
 );%0A%0A

remove hardcoded date