package com.bsis.peuplement.controler;

import com.bsis.peuplement.model.Dossier;
import com.bsis.peuplement.service.DossierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping ("/dossiers")
public class DossierController {
    @Autowired
    private DossierService dossierService;
    //.....................................................
    // READ
    //.....................................................
    @GetMapping("/all")
    public ResponseEntity<List<Dossier>> getAll () {
        List<Dossier> dossiers = dossierService.getAll();
        return new ResponseEntity<>(dossiers, HttpStatus.OK);
    }
    //......................................................
    @RequestMapping("/get/{id}")
    public Optional<Dossier> getDossierById(@PathVariable("id") Long id) {
        return dossierService.getById(id);
    }
    //.....................................................
    @RequestMapping("/count")
    public long count() {
        return dossierService.count();
    }
    //......................................................................
    //                 Update
    //......................................................................
    @PutMapping("/update")
    public ResponseEntity<Dossier> update(@RequestBody Dossier dossier) {
        Dossier updateDossier = dossierService.update(dossier);
        return new ResponseEntity<>(updateDossier, HttpStatus.OK);
    }
/*
    @Autowired
    private DossierService dossierService;

    @Autowired
    private RedevableService redevableService;


    //......................................................................
    //                    Create
    //......................................................................
    @PostMapping("/add")
    public ResponseEntity<Dossier> addDossier(@RequestBody Dossier dossier) {
        Dossier newDossier = dossierService.add(dossier);
        return new ResponseEntity<>(newDossier, HttpStatus.CREATED);
    }
    //...
    //......................................................................
    //                    Read
    //......................................................................
    @RequestMapping("/get/{id}")
    public Optional<Dossier> getDossierById(@PathVariable("id") String id) {
        return dossierService.getById(id);
    }
    //......................................................................
    @RequestMapping("/getid/{dossierId}")
    public Dossier getDossierByDossierId(@PathVariable("dossierId") String dossierId) {
        return dossierService.getByDossierId(dossierId);
    }
    //......................................................................
    @GetMapping("/all")
    public ResponseEntity<List<Dossier>> getAll () {
        List<Dossier> dossiers = dossierService.getAll();
        return new ResponseEntity<>(dossiers, HttpStatus.OK);
    }
    //......................................................................
    //......................................................................
    @RequestMapping("/count")
    public long count() {
        return dossierService.count();
    }
    //......................................................................
    //                 Update
    //......................................................................
    @PutMapping("/update")
    public ResponseEntity<Dossier> update(@RequestBody Dossier dossier) {
        Dossier updateDossier = dossierService.update(dossier);
        return new ResponseEntity<>(updateDossier, HttpStatus.OK);
    }
    //......................................................................
    //                 UpdateSiret
    //......................................................................
    @PutMapping("/updateSiret/{siret}")
    ResponseEntity<List<Dossier>> updateSiret(@PathVariable("siret") String siret ) {
//        Dossier updateDossier = dossierService.update(dossier);
        List<Dossier> dossiers = dossierService.getAll();
        for (Dossier dossier:dossiers)
        {
            dossier.setSiret(siret);
        }
        return new ResponseEntity<>(dossiers, HttpStatus.OK);
    }
    //......................................................................
    //                 Delete
    //......................................................................
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> delete(@PathVariable("id") String id) {
        dossierService.delete(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    //......................................................................
    @RequestMapping("/deleteAll")
    public ResponseEntity<?> deleteAll() {
        dossierService.deleteAll();
        return new ResponseEntity<>(HttpStatus.OK);
    }
    //......................................................................
    //                 Recherche
    //......................................................................
    @RequestMapping("/getXYStorAdr")
    public String []  getXYStorAdr(@RequestBody Dossier dossier) {
        return dossierService.xyStorAdr(dossier);

    }
    //....................................................
    // distance
    //....................................................
    public static void writeToFile(String logFile, String content) throws IOException {
        try (FileWriter fileWriter = new FileWriter(logFile, true)) {
            fileWriter.write(content);
        }
    }
    @RequestMapping("/distance")
    public double  distance(@RequestBody Dossier dossier) {

        //.........................................
        //.........................................

        String[] point1 = dossierService.xyStorAdr(dossier);
        String[] point2 = redevableService.xyFichierPlat(dossier,"");
        String logFile = "/csvFiles/log.txt";

        try {
            FileWriter fileWriter = new FileWriter(logFile, false);
            fileWriter.write("");

            // Écriture dans le fichier pour la première fois
            writeToFile(logFile, "DossierId:"+dossier.getDossierId()+"|Adr:"+dossier.getAdresse()+"|XYStrAdr X:"+point1[0]+" Y:"+point1[1]+"|RedevableId:"+dossier.getRedevableId()+"|Siret:"+redevableService.getByRedevableId(dossier.getRedevableId()).getCodeSiret()+"|XYFlatFile X:"+point2[0]+" Y:"+point2[1]+"|Distance="+dossierService.distance(point1[0],point1[1],point2[0],point2[1])+"\n");

            // Écriture dans le fichier pour la deuxième fois
            writeToFile(logFile, "..................................................................................................................................................................................................................................\n");


            System.out.println("Success writing Log File.");
        } catch (IOException e) {
            System.out.println("Error while writting log file : " + logFile);
            e.printStackTrace();
        }

        return dossierService.distance(point1[0],point1[1],point2[0],point2[1] );
        //........................................................................
    }
    @RequestMapping("/peuplement/all")
    public List<DossierDistance>  peuplementAll() {
        return dossierService.peuplementAll();
    }
    //..........................................................
    // Filtrer fichier Plat
    //.........................................................
    @RequestMapping("/flatFileFiltrer")
    public String  FlatFileFiltrer() {
        return dossierService.FlatFileFiltrer();
//
    }
    //............................................................
    @RequestMapping("/peuplement/{dossierId}")
    public DossierDistance peuplementById(@PathVariable("dossierId") String dossierId) {
        return dossierService.peuplementById(dossierId);
    }
    //.............................................................
    @RequestMapping("/peuplement/set/{set}")
    public List<DossierDistance>  peuplementSet(@PathVariable("set") String set)
    {
        return dossierService.peuplementSet(set);
    }
    //.............................................................
    @RequestMapping("/peuplement/init")
    public String init ()  {
        return dossierService.init();
    }*/
}
