/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webservices;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import tmc.mobile.proxy.client.TMCToMobileClient;
import tmc.proxy.mobile.custom.request.GetVMSListRequestType;
import tmc.proxy.mobile.custom.request.ReportVehicleLocationRequestType;
import tmc.proxy.mobile.custom.request.WeatherForecastRequestType;
import tmc.response.parser.GetHazardListParser;
import tmc.response.parser.GetVMSListParser;
import tmc.response.parser.PostHazardParser;
import tmc.response.parser.ReportVehicleLocationParser;
import tmc.response.parser.SayHelloParser;
import tmc.response.parser.Top5GasPriceParser;
import tmc.response.parser.WeatherForecastParser;
import tmc.ws.expose.CoordinateType;
import tmc.ws.expose.GetHazardListRequestType;
import tmc.ws.expose.HazardCategoryType;
import tmc.ws.expose.HazardType;
import tmc.ws.expose.HelloWorldRequestType;
import tmc.ws.expose.LocationType;
import tmc.ws.expose.PostHazardRequestType;
import tmc.ws.expose.Top5GasPriceRequestType;
import tmc.ws.expose2.GasPriceType;
import tmc.ws.expose2.GetHazardListResponseType;
import tmc.ws.expose2.GetVMSListResponseType;
import tmc.ws.expose2.HazardListType;
import tmc.ws.expose2.HelloWorldResponseType;
import tmc.ws.expose2.PostHazardResponseType;
import tmc.ws.expose2.ReportVehicleLocationResponseType;
import tmc.ws.expose2.Top5GasPriceResponseType;
import tmc.ws.expose2.VMSMessage;
import tmc.ws.expose2.WeatherForecastResponseType;

/**
 *
 * @author Student
 */
public class WebServiceCalls {

    public static TMCToMobileClient proxyClient = new TMCToMobileClient("http://10.2.2.3:8080/tmc-dev/servlet/tmc.proxy.mobile.TMCToMobileServlet");

    public static String HelloWorld() {
        String result = "";
        HelloWorldRequestType request = new HelloWorldRequestType();
        request.setHelloStr("World From Vinay!");
        try {

            String response = proxyClient.sayHello(request);
            SayHelloParser txt = new SayHelloParser(response);
            HelloWorldResponseType responseObject = txt.parse();
            result = responseObject.getHelloStr();

        } catch (Exception ex) {
            System.out.println(ex.toString());
            result = "";
        }
        return result;
    }

    public static List<tmc.ws.expose2.HazardType> getHazardList(Double lat, Double lon) {

        GetHazardListRequestType request = new GetHazardListRequestType();
        LocationType location = new LocationType();
        CoordinateType coordinates = new CoordinateType();
        coordinates.setLatitude(lat);
        coordinates.setLongitude(lon);
        location.setCoordinates(coordinates);
        request.setLocation(location);

        List<tmc.ws.expose2.HazardType> response_l = new ArrayList<tmc.ws.expose2.HazardType>();

        try {

            String response = proxyClient.getHazardList(request);
            GetHazardListParser txt = new GetHazardListParser(response);
            GetHazardListResponseType responseObject = txt.parse();
            HazardListType result = responseObject.getHazardList();
            for (int i = 0; i < result.getHazard().size(); i++) {
                response_l.add((tmc.ws.expose2.HazardType) result.getHazard().elementAt(i));
            }

        } catch (Exception ex) {
        }

        return response_l;
    }

    public static boolean postHazard(Double lat, Double lon, String code) {
        boolean result = false;
        PostHazardRequestType request = new PostHazardRequestType();
        HazardType hazard = new HazardType();
        LocationType location = new LocationType();
        CoordinateType coordinates = new CoordinateType();
        coordinates.setLatitude(lat);
        coordinates.setLongitude(lon);
        location.setCoordinates(coordinates);
        HazardCategoryType category = new HazardCategoryType();
        category.setCode(code);
        hazard.setLocation(location);
        hazard.setCategory(category);
        request.setHazard(hazard);

        try {
            String response = proxyClient.postHazard(request);
            PostHazardParser txt = new PostHazardParser(response);
            PostHazardResponseType responseObject = txt.parse();
            result = responseObject.isSuccess();
        } catch (Exception ex) {
            result = false;
        }

        return result;

    }

    public static boolean reportVehicleLocation(Double lat, Double lon, String vin) {

        boolean result = false;
        ReportVehicleLocationRequestType request = new ReportVehicleLocationRequestType();
        LocationType location = new LocationType();
        CoordinateType coordinates = new CoordinateType();
        coordinates.setLatitude(lat);
        coordinates.setLongitude(lon);
        location.setCoordinates(coordinates);
        request.setLocation(location);
        request.setVIN(vin);

        try {
            String response = proxyClient.reportVehicleLocation(request);
            ReportVehicleLocationParser txt = new ReportVehicleLocationParser(response);
            ReportVehicleLocationResponseType responseObject = txt.parse();
            result = responseObject.isSuccess();
        } catch (Exception ex) {
            result = false;
        }

        return result;
    }

    public static List<VMSMessage> getVMS(String vin) {
        String result = "";
        GetVMSListRequestType request = new GetVMSListRequestType();
        //String date = Calendar.getInstance().getTime().toString();

        String date = "03-11-2010 17:07:22";//new Date().toString();



        System.out.println(date);
        request.setCurrentTime(date);
        request.setVin(vin);

        List response = new ArrayList();
        try {
            result = proxyClient.getVMSList(request);
            GetVMSListParser txt = new GetVMSListParser(result);
            GetVMSListResponseType responseObject = txt.parse();
            for (int i = 0; i < responseObject.getMessageList().getMessage().size(); i++) {
                response.add((VMSMessage) responseObject.getMessageList().getMessage().elementAt(i));
            }
        } catch (Exception ex) {
            System.out.println("Exception " + ex);
        }

        return response;

    }

    public static List getWeatherForecast(Double lat, Double lon) {
        String response = "";
        WeatherForecastRequestType request = new WeatherForecastRequestType();
        tmc.ws.expose.LocationType location = new tmc.ws.expose.LocationType();
        tmc.ws.expose.CoordinateType coordinates = new tmc.ws.expose.CoordinateType();
        coordinates.setLatitude(lat.doubleValue());
        coordinates.setLongitude(lon.doubleValue());
        location.setCoordinates(coordinates);
        request.setLocation(location);
        Date date = new Date();
        double d = Double.longBitsToDouble(date.getTime());
        request.setCurrentDate(d);
        List responseWithDTO = new ArrayList();
        try {
            response = proxyClient.getWeatherForecast(request);
            System.out.println(response);
            WeatherForecastParser txt = new WeatherForecastParser(response);
            WeatherForecastResponseType responseObject = txt.parse();

            for (int i = 0; i < responseObject.getWeather().size(); i++) {
                WeatherForecastResponseType.Weather weather =
                        (WeatherForecastResponseType.Weather) responseObject.getWeather().elementAt(i);
                System.out.println("DATE: " + weather.getStartTime());
                WeatherDTO dto = new WeatherDTO();
                dto.setEndTime(weather.getEndTime());
                String tmpDate = weather.getStartTime();
                
                int beforeT = tmpDate.indexOf("T");
                String finalDate = tmpDate.substring(0, beforeT);

                dto.setStartTime(finalDate);
                dto.setMaxTemperature(weather.getMaxTemperature());
                dto.setMinTemperature(weather.getMinTemperature());

                dto.setWeatherSummary(weather.getWeatherCondition().getWeatherSummary());
                responseWithDTO.add(dto);
            }


        } catch (Exception ex) {
            System.out.println("ERROR IN WEATHER: " + ex);
        }
        return responseWithDTO;
    }

    public static List getGasPrices(Double lat, Double lon) {
        String result = "";

        Top5GasPriceRequestType request = new Top5GasPriceRequestType();
        tmc.ws.expose.LocationType location = new tmc.ws.expose.LocationType();
        tmc.ws.expose.CoordinateType coordinates = new tmc.ws.expose.CoordinateType();
        coordinates.setLatitude(lat.doubleValue());
        coordinates.setLongitude(lon.doubleValue());
        location.setCoordinates(coordinates);
        request.setLocation(location);
        List responseWithDTO = new ArrayList();

        try {
            result = proxyClient.getTop5GasPrice(request);
            Top5GasPriceParser txt = new Top5GasPriceParser(result);
            Top5GasPriceResponseType responseObject = txt.parse();
            for (int i = 0; i < responseObject.getTop5GasStations().getGasStation().size(); i++) {

                GasPriceType gt = (GasPriceType) responseObject.getTop5GasStations().getGasStation().elementAt(i);
                FuelDTO fdto = new FuelDTO();
                fdto.setStationName(gt.getStationName());
                fdto.setPrice(gt.getPrice());
                fdto.setDate(gt.getDate());
                fdto.setAddress1(gt.getAddressLine01());
                fdto.setAddress2(gt.getAddressLine02());
                responseWithDTO.add(fdto);
               
            }
        } catch (Exception ex) {
        }

        return responseWithDTO;
    }
}
