package bj.pranie.controller;

import bj.pranie.dao.ReservationDao;
import bj.pranie.dao.WashTimeDao;
import bj.pranie.entity.Reservation;
import bj.pranie.entity.WashTime;
import bj.pranie.model.WmModel;
import bj.pranie.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Sebastian Sokolowski on 12.10.16.
 */
@Controller
@RequestMapping(value = "/wm")
public class WmController {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");

    @Autowired
    private ReservationDao reservationDao;

    @Autowired
    private WashTimeDao washTimeDao;

    @RequestMapping(path = "/{year}/{month}/{day}/{washTimeId}", method = RequestMethod.GET)
    public String wm(@PathVariable int year,
                     @PathVariable int month,
                     @PathVariable int day,
                     @PathVariable long washTimeId,
                     Model model) throws ParseException {
        Date date = dateFormat.parse(year + "/" + month + "/" + day);

        WashTime washTime = washTimeDao.findOne(washTimeId);
        List<Reservation> reservationList = reservationDao.findByWashTimeIdAndDate(washTimeId, new java.sql.Date(date.getTime()));
        int wmFree = 3 - reservationList.size();

        model.addAttribute("dayName", getDayName(date));
        model.addAttribute("date", dateFormat.format(date));
        model.addAttribute("time", getWashTime(washTime));
        model.addAttribute("wmFree", wmFree);
        model.addAttribute("reservations", getWmModels(reservationList, date, washTime));
        return "wm/wm";
    }

    private String getWashTime(WashTime washTime) {
        return washTime.getFromTime() + " - " + washTime.getToTime();
    }

    private List<WmModel> getWmModels(List<Reservation> reservationList, Date date, WashTime washTime) {
        List<WmModel> wmModels = new ArrayList<>();

        List<Integer> brokenWm = getBrokenWm();
        boolean isPast = TimeUtil.isPast(timeFormat.format(washTime.getFromTime()), dateFormat.format(date));

        for(int i=0;i!=3;i++){
            WmModel wmModel = new WmModel();

            Reservation reservation = null;
            for (Reservation reservation1: reservationList
                 ) {
                if(reservation1.getWm() == i){
                    reservation = reservation1;
                    break;
                }
            }

            if(reservation == null){
                if(isPast){
                    wmModel.setType(WmModel.TYPE.PAST);
                    wmModel.setColor("#FF0000");
                }else if(brokenWm.contains(i)){
                    wmModel.setType(WmModel.TYPE.BROKEN);
                    wmModel.setColor("#FF0000");
                }else{
                    wmModel.setType(WmModel.TYPE.FREE);
                    wmModel.setColor("#1E9600");
                }
            }else{
                if(isMyReservation()){
                    wmModel.setColor("#FFF200");
                }else{
                    wmModel.setColor("#FF0000");
                    wmModel.setType(WmModel.TYPE.RESERVED);
                    wmModel.setUser(reservation.getUser());
                }
            }

            wmModels.add(wmModel);
        }

        return wmModels;
    }

    private boolean isMyReservation() {
        return false;
    }

    private List<Integer> getBrokenWm(){
        return new ArrayList<>();
    }

    private boolean isRegistrationAvailable(){
        return false;
    }

    private boolean isPast(){
        return false;
    }

    private String getDayName(Date date) {
        Calendar calendar = TimeUtil.getCalendar();
        calendar.setTime(date);

        switch (calendar.get(Calendar.DAY_OF_WEEK)) {
            case 2:
                return "Poniedziałek";
            case 3:
                return "Wtorek";
            case 4:
                return "Środa";
            case 5:
                return "Czwartek";
            case 6:
                return "Piątek";
            case 7:
                return "Sobota";
            case 1:
                return "Niedziela";
            default:
                return "Null";

        }
    }
}

@@ -2309,16 +2309,34 @@
  return 
+timeFormat.format(
 washTime
@@ -2349,16 +2349,17 @@
 omTime()
+)
  + %22 - %22
@@ -2361,16 +2361,34 @@
 %22 - %22 + 
+timeFormat.format(
 washTime
@@ -2399,16 +2399,17 @@
 ToTime()
+)
 ;%0A    %7D%0A
@@ -2741,26 +2741,34 @@
  for
+ 
 (int i
-=0;i
+ = 0; i 
 !=
+ 
 3;
+ 
 i++)
+ 
 %7B%0A  
@@ -2896,16 +2896,17 @@
 rvation1
+ 
 : reserv
@@ -2932,16 +2932,19 @@
         
+  
+ 
 ) %7B%0A    
@@ -2949,32 +2949,33 @@
               if
+ 
 (reservation1.ge
@@ -2985,16 +2985,17 @@
 () == i)
+ 
 %7B%0A      
@@ -3110,16 +3110,17 @@
       if
+ 
 (reserva
@@ -3132,16 +3132,17 @@
 == null)
+ 
 %7B%0A      
@@ -3157,16 +3157,17 @@
   if
+ 
 (isPast)
 %7B%0A  
@@ -3162,16 +3162,17 @@
 (isPast)
+ 
 %7B%0A      
@@ -3291,15 +3291,17 @@
    %7D
+ 
 else if
+ 
 (bro
@@ -3318,16 +3318,17 @@
 ains(i))
+ 
 %7B%0A      
@@ -3437,36 +3437,38 @@
                %7D
+ 
 else
+ 
 %7B%0A              
@@ -3581,36 +3581,38 @@
  %7D%0A            %7D
+ 
 else
+ 
 %7B%0A              
@@ -3615,16 +3615,17 @@
       if
+ 
 (isMyRes
@@ -3635,16 +3635,17 @@
 ation())
+ 
 %7B%0A      
@@ -3704,20 +3704,22 @@
        %7D
+ 
 else
+ 
 %7B%0A      
@@ -4100,16 +4100,17 @@
 okenWm()
+ 
 %7B%0A      
@@ -4189,16 +4189,17 @@
 ilable()
+ 
 %7B%0A      
@@ -4249,16 +4249,17 @@
 isPast()
+ 
 %7B%0A      

Change time format. Format code.