package com.m2sdl.undercooked;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.m2sdl.undercooked.services.UndercookedService;

import java.io.File;

public class FileCheckActivity extends AppCompatActivity {

    private UndercookedService service;

    private ProgressBar progressBar;
    private TextView progressText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filecheck);

        service = new UndercookedService();

        progressBar = findViewById(R.id.progressBar);
        progressText = findViewById(R.id.progressText);

        checkFiles();
    }

    private void checkFiles() {

        File mediaDir = getExternalMediaDirs()[0];

        File mapFile = new File(mediaDir, "midi-pyrenees.map");
        File poiFile = new File(mediaDir, "midi-pyrenees.poi");

        if (mapFile.exists() && poiFile.exists()) {
            startMapActivity();
        } else {
            downloadMapFile(mapFile, poiFile);
        }
    }

    private void downloadMapFile(File mapFile, File poiFile) {

        service.downloadFileFromServer("data/midi-pyrenees.map", mapFile,
                new UndercookedService.OnFileDownloaded() {

                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onProgress(int percent) {
                        runOnUiThread(() -> {
                            progressBar.setProgress(percent);
                            progressText.setText("Téléchargement MAP : " + percent + "%");
                        });
                    }

                    @Override
                    public void onDownloaded() {
                        downloadPoiFile(poiFile);
                    }

                    @Override
                    public void onError(Exception e) {
                        e.printStackTrace();
                    }
                });
    }

    private void downloadPoiFile(File poiFile) {

        service.downloadFileFromServer("data/midi-pyrenees.poi", poiFile,
                new UndercookedService.OnFileDownloaded() {

                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onProgress(int percent) {
                        runOnUiThread(() -> {
                            progressBar.setProgress(percent);
                            progressText.setText("Téléchargement POI : " + percent + "%");
                        });
                    }

                    @Override
                    public void onDownloaded() {
                        runOnUiThread(() -> startMapActivity());
                    }

                    @Override
                    public void onError(Exception e) {
                        e.printStackTrace();
                    }
                });
    }

    private void startMapActivity() {
        Intent intent = new Intent(this, MapActivity.class);
        startActivity(intent);
        finish();
    }
}