/*
 * @author Javier Martin Manzano
 */

package ub.incendishab;

import com.pengrad.telegrambot.Callback;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.BaseRequest;
import com.pengrad.telegrambot.request.SendDocument;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.request.SendPhoto;
import com.pengrad.telegrambot.response.BaseResponse;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;

public class Bot {

    private static Bot myself;
    private TelegramBot bot;
    private String chatId;
    private String response = "";


    public static Bot getBot(){
        if (myself==null){
            myself = new Bot();
        }
        return myself;
    }

    private Bot(){
        String token ="675006415:AAH0aANGFlcB1l88yDMv1VJnBSgX8W1k9FU";
        chatId = "-377128731";
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();

        bot = new TelegramBot.Builder(token).okHttpClient(client).build();
    }

    public String getResponse() {
        return response;
    }

    public void deleteResponse() {
        response = "";
    }

    public void     sendMessageRequest(String message){
        SendMessage request = new SendMessage(chatId, message)
                .parseMode(ParseMode.HTML)
                .disableWebPagePreview(true)
                .disableNotification(false);

        bot.execute(request, new Callback() {
            @Override
            public void onResponse(BaseRequest baseRequest, BaseResponse baseResponse) {
                response = baseResponse.toString();
            }

            @Override
            public void onFailure(BaseRequest baseRequest, IOException e) {
                response = e.toString();
            }
        });
    }

    public void sendDocumentRequest(File file, String caption){
        SendDocument request = new SendDocument(chatId,file)//SendPhoto(chatId, file)
                .parseMode(ParseMode.HTML)
                .disableNotification(false)
                .caption(caption);

        bot.execute(request, new Callback() {
            @Override
            public void onResponse(BaseRequest baseRequest, BaseResponse baseResponse) {
                response = baseResponse.toString();
            }

            @Override
            public void onFailure(BaseRequest baseRequest, IOException e) {
                response = e.toString();
            }
        });
    }

    public void sendPhotoRequest(File photoFile, String caption){
        SendPhoto request = new SendPhoto(chatId, photoFile)
                .parseMode(ParseMode.HTML)
                .disableNotification(false)
                .caption(caption);

        bot.execute(request, new Callback() {
            @Override
            public void onResponse(BaseRequest baseRequest, BaseResponse baseResponse) {
                response = baseResponse.toString();
            }

            @Override
            public void onFailure(BaseRequest baseRequest, IOException e) {
                response = e.toString();
            }
        });
    }

    public void setChatId(String chatId){
        this.chatId = chatId;
    }
}
