/*
 * @author Javier Martin Manzano
 */

package ub.incendishab;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Area extends AppCompatActivity {


    private int xOffsetText;
    private int yOffsetText;

    private int xOffsetPerson;
    private int yOffsetPerson;
    private int xOffsetPersonToPerson;

    private int xOffsetMini;
    private int yOffsetMini;
    private int xOffsetMiniToPerson;

    private Activity thisActivity;

    private Bitmap bitmap, peopleBitmap;
    private ImageView imageView, peopleView;
    private DataManager dataManager;

    private boolean dragFromCanvas = false;

    private Paint paint, paintText;
    private Canvas canvasPeople,canvas;

    private boolean hospital = false;
    private boolean evacuados = false;

    private int altura,longitud, escalera;

    private int quant;


    private View.OnClickListener returnListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent resultIntent = new Intent();
            setResult(RESULT_OK, resultIntent);
            thisActivity.finish();
        }
    };

    private View.OnClickListener observationsListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(thisActivity);
            alertDialog.setTitle("OBSERVACIONES");
            final EditText obs = new EditText(thisActivity);
            obs.setText(dataManager.readObservations(altura,longitud, escalera));
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
            obs.setLayoutParams(lp);
            alertDialog.setView(obs);

            alertDialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            alertDialog.setPositiveButton("Guardar", new DialogInterface.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                public void onClick(DialogInterface dialog, int which) {
                    dataManager.setObservations(altura,longitud,obs.getText().toString(), escalera);
                }
            });

            alertDialog.show();
        }
    };


    private View.OnTouchListener onTouchListenerCanvas = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent ev){
            float x, y;

            int action = (ev.getAction() & MotionEvent.ACTION_MASK);
            switch (action){
                case MotionEvent.ACTION_DOWN:
                    boolean personExists = false;
                    int personTypes = dataManager.confInApartment(altura, longitud, escalera);
                    String personType = null;
                    int quant = 0;
                    View view = null;
                    Bitmap bm = BitmapFactory.decodeResource(thisActivity.getResources(),R.drawable.piso);
                    float zoom = (float)(v.getWidth())/bm.getWidth();
                    x = ev.getX()/zoom;
                    y = ev.getY()/zoom;

                    bm = BitmapFactory.decodeResource(thisActivity.getResources(),R.drawable.personokcard);
                    for (int i = 0; i < personTypes; i++) {
                        if (x >= (xOffsetPerson + (i * xOffsetPersonToPerson)) && y >= (yOffsetPerson) && x <= (xOffsetPerson + (i * xOffsetPersonToPerson) + bm.getWidth()) && y <= (yOffsetPerson + bm.getHeight())) {
                            personExists = true;
                            //personType = dataManager.readPersonType(altura, longitud, i);
                            quant = dataManager.readConfQuant(altura, longitud, i, escalera);
                            //index = i;
                            if (i == 1) {
                                personType = "z4";
                            } else if (i == 2) {
                                personType = "z5";
                            } else if (i == 3) {
                                personType = "z6";
                            } else {
                                personType = "person";
                            }

                            switch (i) {
                                case 0:
                                    view = findViewById(R.id.okView);
                                    break;
                                case 1:
                                    view = findViewById(R.id.z4View);
                                    break;
                                case 2:
                                    view = findViewById(R.id.z5View);
                                    break;
                                case 3:
                                    view = findViewById(R.id.z6View);
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                    //Si estoy pulsando sobre una persona
                    if (personExists) {
                        ClipData data = ClipData.newPlainText("persontype", personType);
                        data.addItem(new ClipData.Item(Integer.toString(quant)));
                        v.startDrag(data, new View.DragShadowBuilder(view), null, 0);
                        dataManager.removePerson(altura, longitud, escalera, personType);
                        drawPeopleInApartment(altura, longitud);
                        dragFromCanvas = true;
                        return true;
                    }
                    break;

                default:
                    break;
            }

            return false;
        }
    };

    private View.OnDragListener onDragListenerCanvas = new View.OnDragListener() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public boolean onDrag(View v, DragEvent ev){
            final int action = ev.getAction();
            switch (action){
                case DragEvent.ACTION_DRAG_STARTED:
                    return true;

                case DragEvent.ACTION_DRAG_ENTERED:
                    return true;

                case DragEvent.ACTION_DRAG_LOCATION:
                    return true;

                case DragEvent.ACTION_DRAG_EXITED:
                    return true;

                case DragEvent.ACTION_DROP:
                    String personType = ev.getClipData().getItemAt(0).getText().toString();
                    int quant = Integer.parseInt(ev.getClipData().getItemAt(1).getText().toString());
                    dragFromCanvas = false;
                    dropPerson(personType,quant);
                    return true;

                case DragEvent.ACTION_DRAG_ENDED:
                    if (dragFromCanvas) {
                        dataManager.addConfPerson(altura,longitud, escalera);
                        drawPeopleInApartment(altura,longitud);
                        dragFromCanvas = false;
                    }
                    return true;

                default:
                    break;
            }
            return false;
        }
    };

    private View.OnTouchListener onTouchListenerPerson = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent ev){
            ClipData data = ClipData.newPlainText("personType", (v.getResources().getResourceName(v.getId())).replace("View", "").replace("ub.incendishab:id/", ""));
            data.addItem(new ClipData.Item("1"));
            v.startDrag(data, new View.DragShadowBuilder(v), null, 0);
            return true;
        }
    };


    private View.OnDragListener onDragListenerPerson = new View.OnDragListener() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public boolean onDrag(View v, DragEvent ev){
            final int action = ev.getAction();
            switch (action){
                case DragEvent.ACTION_DRAG_STARTED:
                    return true;

                case DragEvent.ACTION_DRAG_ENTERED:
                    return true;

                case DragEvent.ACTION_DRAG_LOCATION:
                    return true;

                case DragEvent.ACTION_DRAG_EXITED:
                    return true;

                case DragEvent.ACTION_DROP:
                    dragFromCanvas = false;
                    drawPeople(peopleView);
                    reDrawEvac();
                    reDrawHosp();
                    return true;

                case DragEvent.ACTION_DRAG_ENDED:
                    return true;

                default:
                    break;
            }
            return false;
        }
    };

    private View.OnDragListener onDragListenerArea = new View.OnDragListener() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public boolean onDrag(View v, DragEvent ev){
            final int action = ev.getAction();
            switch (action){
                case DragEvent.ACTION_DRAG_STARTED:
                    return true;

                case DragEvent.ACTION_DRAG_ENTERED:
                    ((ImageView)v).setColorFilter(Color.LTGRAY, PorterDuff.Mode.LIGHTEN);
                    return true;

                case DragEvent.ACTION_DRAG_LOCATION:
                    return true;

                case DragEvent.ACTION_DRAG_EXITED:
                    ((ImageView)v).setColorFilter(Color.TRANSPARENT);
                    return true;

                case DragEvent.ACTION_DROP:
                    ((ImageView)v).setColorFilter(Color.TRANSPARENT);
                    if (dragFromCanvas) {
                        if (v.getId()==R.id.evacView) {
                            dataManager.addEvacPerson(altura,longitud,escalera);
                        } else if (v.getId()==R.id.hospView) {
                            dataManager.addHospPerson(altura,longitud,escalera);
                        }
                        dragFromCanvas = false;
                    }
                    drawPeople(peopleView);
                    drawArea(imageView);
                    reDrawEvac();
                    reDrawHosp();
                    return true;

                case DragEvent.ACTION_DRAG_ENDED:
                    return true;

                default:
                    break;
            }
            return false;
        }
    };

    private View.OnTouchListener onTouchListenerPeople = new View.OnTouchListener() {

        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                float[] coords = new float[2];
                Bitmap bm = BitmapFactory.decodeResource(thisActivity.getResources(), R.drawable.bordepersonas);
                float zoom = (float) (v.getWidth()) / (bm.getWidth() + 10);
                coords[0] = event.getX() / zoom;
                coords[1] = event.getY() / zoom;
                int p = (int) (coords[1] / yOffsetPerson);
                if (p>=quant) {
                    return false;
                }
                int[] personCoords;
                if (coords[0] > 5 + xOffsetPersonToPerson && coords[0] < 5 + xOffsetPersonToPerson * 2) {
                    //Age
                    if (evacuados) {
                        personCoords = findEvacPerson(p);
                    } else if (hospital){
                        personCoords = findHospPerson(p);
                    } else {
                        personCoords = findPerson(p);
                    }
                    String prevAge, newAge;
                    switch (personCoords[0]) {
                        case 0:
                            //confinados
                            prevAge = dataManager.readConfAge(altura, longitud, escalera, personCoords[1], personCoords[2]);
                            newAge = newAge(prevAge);
                            dataManager.editConfAge(altura, longitud, escalera, personCoords[1], personCoords[2], newAge);
                            break;
                        case 1:
                            //evacuados
                            if (evacuados){
                                prevAge = dataManager.readEvacAge(personCoords[3], personCoords[4], escalera, personCoords[1], personCoords[2]);
                                newAge = newAge(prevAge);
                                dataManager.editEvacAge(personCoords[3], personCoords[4], escalera, personCoords[1], personCoords[2], newAge);
                            } else {
                                prevAge = dataManager.readEvacAge(altura, longitud, escalera, personCoords[1], personCoords[2]);
                                newAge = newAge(prevAge);
                                dataManager.editEvacAge(altura, longitud, escalera, personCoords[1], personCoords[2], newAge);
                            }

                            break;
                        case 2:
                            //hospitalizados
                            if (hospital){
                                prevAge = dataManager.readHospAge(personCoords[3], personCoords[4], escalera, personCoords[1], personCoords[2]);
                                newAge = newAge(prevAge);
                                dataManager.editHospAge(personCoords[3], personCoords[4], escalera, personCoords[1], personCoords[2], newAge);
                            } else {
                                prevAge = dataManager.readHospAge(altura, longitud, escalera, personCoords[1], personCoords[2]);
                                newAge = newAge(prevAge);
                                dataManager.editHospAge(altura, longitud, escalera, personCoords[1], personCoords[2], newAge);
                            }

                            break;
                        default:
                            break;
                    }
                    if (evacuados || hospital){
                        drawEvacPeople(peopleView);
                    } else {
                        drawPeople(peopleView);
                    }
                    v.invalidate();
                    return true;
                } else if (coords[0] > 5 + xOffsetPersonToPerson * 2 && coords[0] < 5 + xOffsetPersonToPerson * 3) {
                    //Special
                    if (evacuados) {
                        personCoords = findEvacPerson(p);
                    } else if (hospital){
                        personCoords = findHospPerson(p);
                    } else {
                        personCoords = findPerson(p);
                    }
                    String prevSpec, newSpec;
                    switch (personCoords[0]) {
                        case 0:
                            //confinados
                            prevSpec = dataManager.readConfSpec(altura, longitud, escalera, personCoords[1], personCoords[2]);
                            newSpec = newSpecial(prevSpec);
                            dataManager.editConfSpec(altura, longitud, escalera, personCoords[1], personCoords[2], newSpec);
                            break;
                        case 1:
                            //evacuados
                            if (evacuados){
                                prevSpec = dataManager.readEvacSpec(personCoords[3], personCoords[4], escalera, personCoords[1], personCoords[2]);
                                newSpec = newSpecial(prevSpec);
                                dataManager.editEvacSpec(personCoords[3], personCoords[4], escalera, personCoords[1], personCoords[2], newSpec);
                            } else {
                                prevSpec = dataManager.readEvacSpec(altura, longitud, escalera, personCoords[1], personCoords[2]);
                                newSpec = newSpecial(prevSpec);
                                dataManager.editEvacSpec(altura, longitud, escalera, personCoords[1], personCoords[2], newSpec);
                            }
                            break;
                        case 2:
                            //hospitalizados
                            if (hospital){
                                prevSpec = dataManager.readHospSpec(personCoords[3], personCoords[4], escalera, personCoords[1], personCoords[2]);
                                newSpec = newSpecial(prevSpec);
                                dataManager.editHospSpec(personCoords[3], personCoords[4], escalera, personCoords[1], personCoords[2], newSpec);
                            } else {
                                prevSpec = dataManager.readHospSpec(altura, longitud, escalera, personCoords[1], personCoords[2]);
                                newSpec = newSpecial(prevSpec);
                                dataManager.editHospSpec(altura, longitud, escalera, personCoords[1], personCoords[2], newSpec);
                            }
                            break;
                        default:
                            break;
                    }
                    if (evacuados || hospital){
                        drawEvacPeople(peopleView);
                    } else {
                        drawPeople(peopleView);
                    }
                    v.invalidate();
                    return true;
                } else if (coords[0] > 5 + xOffsetPersonToPerson * 3 && coords[0] < 5 + xOffsetPersonToPerson * 4){
                    //hospital
                    if (evacuados) {
                        personCoords = findEvacPerson(p);
                    } else if (hospital){
                        personCoords = findHospPerson(p);
                    } else {
                        personCoords = findPerson(p);
                    }
                    if (personCoords[0]==2){
                        modHospitalText(personCoords);
                    }
                }
            }
            return false;
        }
    };


    @TargetApi(Build.VERSION_CODES.KITKAT)
    protected void onCreate(Bundle savedInstanceState) {
        thisActivity = this;
        super.onCreate(savedInstanceState);
        Bundle b = getIntent().getExtras();
        setContentView(R.layout.activity_area);
        setOffsets();
        Button button = findViewById(R.id.obsButton);
        button.setOnClickListener(observationsListener);
        Button button2 = findViewById(R.id.returnButton);
        button2.setOnClickListener(returnListener);

        String direccion = b.getString("direccion");
        dataManager = new DataManager(direccion);
        if (b.containsKey("evac")) {
            evacuados = true;

        } else if (b.containsKey("hosp")){
            hospital = true;
        } else {
            altura = b.getInt("altura");
            longitud = b.getInt("longitud");
            escalera = b.getInt("escalera");
        }

        peopleView = findViewById(R.id.peopleImageView);
        imageView = findViewById(R.id.areaImageView);
        paint = new Paint();
        paintText = new Paint();
        paintText.setTextSize(50);
        paintText.setFakeBoldText(true);

        ImageView okView = findViewById(R.id.okView);
        ImageView z4View = findViewById(R.id.z4View);
        ImageView z5View = findViewById(R.id.z5View);
        ImageView z6View = findViewById(R.id.z6View);
        ImageView areaHosp = findViewById(R.id.hospView);
        ImageView areaEvac = findViewById(R.id.evacView);

        areaEvac.setOnDragListener(onDragListenerArea);
        areaHosp.setOnDragListener(onDragListenerArea);

        peopleView.setOnTouchListener(onTouchListenerPeople);

        okView.setOnTouchListener(onTouchListenerPerson);
        z4View.setOnTouchListener(onTouchListenerPerson);
        z5View.setOnTouchListener(onTouchListenerPerson);
        z6View.setOnTouchListener(onTouchListenerPerson);

        okView.setOnDragListener(onDragListenerPerson);
        z4View.setOnDragListener(onDragListenerPerson);
        z5View.setOnDragListener(onDragListenerPerson);
        z6View.setOnDragListener(onDragListenerPerson);

        imageView.setOnDragListener(onDragListenerCanvas);
        imageView.setOnTouchListener(onTouchListenerCanvas);

        if (evacuados||hospital){
            LinearLayout horLayout = findViewById(R.id.horizontalLayout);
            ((ViewManager)horLayout.getParent()).removeView(horLayout);
            LinearLayout layout = findViewById(R.id.linearLayout);
            layout.removeView(button);
            drawEvacPeople(peopleView);
            drawEvacArea(imageView);
        } else {
            drawPeople(peopleView);
            drawArea(imageView);
            reDrawEvac();
            reDrawHosp();
        }
    }

    private void drawEvacArea(View view){
        int id = 0;
        if (evacuados) {
            id = R.drawable.evacuados;
        } else if (hospital){
            id = R.drawable.hospital;
        }
        Bitmap bm = BitmapFactory.decodeResource(this.getResources(), id);
        bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);

        imageView.setImageBitmap(bitmap);
        canvas = new Canvas(bitmap);
        canvas.drawBitmap(bm, 0, 0, paint);
        drawEvacPeopleInArea();
        view.invalidate();
    }

    private void drawArea(View view){
        String im = dataManager.readImage(altura,longitud, escalera);
        String text = dataManager.readText(altura,longitud, escalera);
        String estado = dataManager.readState(altura,longitud, escalera);

        if (estado.contains("afectado")){
            im = im.concat("_afectado");
        } else if (estado.contains("origen")){
            im = im.concat("_origen");
        }
        if (estado.contains("estructural")){
            im = im.concat("_estructural");
        }

        int id = this.getResources().getIdentifier(im, "drawable", this.getPackageName());
        Bitmap bm = BitmapFactory.decodeResource(this.getResources(), id);
        bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);

        imageView.setImageBitmap(bitmap);
        canvas = new Canvas(bitmap);
        canvas.drawBitmap(bm, 0, 0, paint);
        if (!text.isEmpty()) {
            paintText.setColor(Color.rgb(73, 73, 73));
            if (text.contains("Tejado")) {
                canvas.drawText(text, xOffsetText, yOffsetText * 2, paintText);
            } else {
                canvas.drawText(text, xOffsetText, yOffsetText, paintText);
            }
        }
        drawPeopleInApartment(altura,longitud);
        view.invalidate();
    }

    private void drawEvacPeople(View view){

        quant = 0;
        Bitmap state = null;
        if (evacuados) {
            quant = dataManager.getEvacuados();
            state = BitmapFactory.decodeResource(this.getResources(), R.drawable.evacuado);
        } else if (hospital){
            quant = dataManager.getHospitalizados();
            state = BitmapFactory.decodeResource(this.getResources(), R.drawable.hospitalizado);
        }

        if (quant>0) {
            Bitmap borde = BitmapFactory.decodeResource(this.getResources(), R.drawable.bordepersonas);

            int width = borde.getWidth() + 10;
            int height = yOffsetPerson * quant;

            peopleBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            ((ImageView) (view)).setImageBitmap(peopleBitmap);

            canvasPeople = new Canvas(peopleBitmap);
            int x = 5;
            int y = 5;

            int p = 0;

            String age, spec, text;
            Bitmap ageBitmap, specBitmap, personType;

            paintText = new Paint();
            for (int a = 0; a < dataManager.getAlturaTotal(); a++){
                for (int l = 0; l < dataManager.getLongitudTotal(); l++) {
                    for (int esc = 1; esc < dataManager.getEscaleras() + 1; esc++){
                        if (evacuados) {
                            for (int type = 0; type < dataManager.evacInApartment(a, l, esc); type++) {
                                for (int elem = 0; elem < dataManager.readEvacQuant(a, l, type, esc); elem++) {
                                    age = dataManager.readEvacAge(a, l, esc, type, elem);
                                    spec = dataManager.readEvacSpec(a, l, esc, type, elem);
                                    switch (age) {
                                        case "nino":
                                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.nino);
                                            break;
                                        case "adulto":
                                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.adulto);
                                            break;
                                        case "anciano":
                                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.anciano);
                                            break;
                                        default:
                                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.interrogante);
                                            break;
                                    }

                                    switch (spec) {
                                        case "bombero":
                                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bombero);
                                            break;
                                        case "minusvalido":
                                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.silla_ruedas);
                                            break;
                                        default:
                                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.interrogante);
                                            break;
                                    }

                                    switch (type) {
                                        case 1:
                                            personType = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz4card);
                                            break;
                                        case 2:
                                            personType = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz5card);
                                            break;
                                        case 3:
                                            personType = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz6card);
                                            break;
                                        default:
                                            personType = BitmapFactory.decodeResource(this.getResources(), R.drawable.personokcard);
                                            break;
                                    }
                                    paintText.setTextSize(50);
                                    text = dataManager.readText(a, l, esc);
                                    if (text.isEmpty()) {
                                        text = dataManager.readImage(a, l, esc);
                                        text = text.split("[1-3]")[0];
                                        paintText.setTextSize(40);
                                    } else if (text.length()>5){
                                        paintText.setTextSize(30);
                                    }
                                    canvasPeople.drawBitmap(borde, x, y + (p * yOffsetPerson), paint);
                                    canvasPeople.drawBitmap(personType, x, y + (p * yOffsetPerson), paint);
                                    canvasPeople.drawBitmap(ageBitmap, x + xOffsetPersonToPerson, y + (p * yOffsetPerson), paint);
                                    canvasPeople.drawBitmap(specBitmap, x + xOffsetPersonToPerson * 2, y + (p * yOffsetPerson), paint);
                                    canvasPeople.drawBitmap(state, x + xOffsetPersonToPerson * 3, y + (p * yOffsetPerson), paint);
                                    canvasPeople.drawText(text, x + xOffsetPersonToPerson * 4, y + yOffsetText + 10 + (p * yOffsetPerson), paintText);
                                    if (dataManager.getEscaleras() > 1) {
                                        String escText;
                                        if (esc == 1) {
                                            escText = "escalera A";
                                        } else {
                                            escText = "escalera B";
                                        }
                                        paintText.setTextSize(30);
                                        canvasPeople.drawText(escText, x + xOffsetPersonToPerson * 4, y + yOffsetText + 50 + (p * yOffsetPerson), paintText);
                                    }
                                    p++;
                                }
                            }
                        } else if (hospital) {
                            for (int type = 0; type < dataManager.hospInApartment(a, l, esc); type++) {
                                for (int elem = 0; elem < dataManager.readHospQuant(a, l, type, esc); elem++) {
                                    age = dataManager.readHospAge(a, l, esc, type, elem);
                                    spec = dataManager.readHospSpec(a, l, esc, type, elem);
                                    switch (age) {
                                        case "nino":
                                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.nino);
                                            break;
                                        case "adulto":
                                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.adulto);
                                            break;
                                        case "anciano":
                                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.anciano);
                                            break;
                                        default:
                                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.interrogante);
                                            break;
                                    }

                                    switch (spec) {
                                        case "bombero":
                                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bombero);
                                            break;
                                        case "minusvalido":
                                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.silla_ruedas);
                                            break;
                                        default:
                                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.interrogante);
                                            break;
                                    }

                                    switch (type) {
                                        case 1:
                                            personType = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz4card);
                                            break;
                                        case 2:
                                            personType = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz5card);
                                            break;
                                        case 3:
                                            personType = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz6card);
                                            break;
                                        default:
                                            personType = BitmapFactory.decodeResource(this.getResources(), R.drawable.personokcard);
                                            break;
                                    }
                                    paintText.setTextSize(50);
                                    text = dataManager.readText(a, l, esc);
                                    if (text.isEmpty()) {
                                        text = dataManager.readImage(a, l, esc);
                                        text = text.split("[1-3]")[0];
                                        paintText.setTextSize(40);
                                    } else if (text.length()>5){
                                        paintText.setTextSize(30);
                                    }
                                    canvasPeople.drawBitmap(borde, x, y + (p * yOffsetPerson), paint);
                                    canvasPeople.drawBitmap(personType, x, y + (p * yOffsetPerson), paint);
                                    canvasPeople.drawBitmap(ageBitmap, x + xOffsetPersonToPerson, y + (p * yOffsetPerson), paint);
                                    canvasPeople.drawBitmap(specBitmap, x + xOffsetPersonToPerson * 2, y + (p * yOffsetPerson), paint);
                                    canvasPeople.drawBitmap(state, x + xOffsetPersonToPerson * 3, y + (p * yOffsetPerson), paint);
                                    canvasPeople.drawText(text, x + xOffsetPersonToPerson * 4, y + yOffsetText + 10 + (p * yOffsetPerson), paintText);
                                    if (dataManager.getEscaleras() > 1) {
                                        String escText;
                                        if (esc == 1) {
                                            escText = "escalera A";
                                        } else {
                                            escText = "escalera B";
                                        }
                                        paintText.setTextSize(30);
                                        canvasPeople.drawText(escText, x + xOffsetPersonToPerson * 4, y + yOffsetText + 50 + (p * yOffsetPerson), paintText);
                                    }
                                    p++;
                                }
                            }
                        }
                    }
                }
            }
        }
        view.invalidate();
    }

    private void drawPeople(View view){

        quant = 0;
        int confTypes = dataManager.confInApartment(altura,longitud, escalera);

        for (int i=0;i<confTypes;i++) {
            quant += dataManager.readConfQuant(altura, longitud, i, escalera);
        }

        int evacTypes = dataManager.evacInApartment(altura,longitud, escalera);
        for (int i=0;i<evacTypes;i++) {
            quant += dataManager.readEvacQuant(altura, longitud, i, escalera);
        }

        int hospTypes = dataManager.hospInApartment(altura,longitud, escalera);
        for (int i=0;i<hospTypes;i++) {
            quant += dataManager.readHospQuant(altura, longitud, i, escalera);
        }

        if (quant>0) {
            Bitmap borde = BitmapFactory.decodeResource(this.getResources(), R.drawable.bordepersonas);

            int width = borde.getWidth() + 10;
            int height = yOffsetPerson * quant;

            peopleBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            ((ImageView) (view)).setImageBitmap(peopleBitmap);

            canvasPeople = new Canvas(peopleBitmap);
            Bitmap person;
            int x = 5;
            int y = 5;

            int p = 0;

            Bitmap state = BitmapFactory.decodeResource(this.getResources(), R.drawable.confinado);

            String text = dataManager.readText(altura,longitud, escalera);
            paintText = new Paint();
            paintText.setTextSize(50);
            if (text.isEmpty()){
                text = dataManager.readImage(altura,longitud, escalera);
                text = text.split("[1-3]")[0];
                paintText.setTextSize(40);
            } else if (text.length()>5){
                paintText.setTextSize(30);
            }

            for (int i = 0; i < confTypes; i++) {

                if (i==1) {
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz4card);
                } else if (i==2) {
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz5card);
                } else if (i==3) {
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz6card);
                } else {
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personokcard);
                }

                int stateQuant = dataManager.readConfQuant(altura, longitud, i, escalera);
                String age, spec;
                Bitmap ageBitmap, specBitmap;
                for (int j = 0; j < stateQuant; j++) {
                    age = dataManager.readConfAge(altura,longitud, escalera,i,j);
                    spec = dataManager.readConfSpec(altura,longitud, escalera,i,j);
                    switch (age){
                        case "nino":
                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.nino);
                            break;
                        case "adulto":
                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.adulto);
                            break;
                        case "anciano":
                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.anciano);
                            break;
                        default:
                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.interrogante);
                            break;
                    }

                    switch (spec){
                        case "bombero":
                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bombero);
                            break;
                        case "minusvalido":
                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.silla_ruedas);
                            break;
                        default:
                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.interrogante);
                            break;
                    }

                    canvasPeople.drawBitmap(borde, x, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawBitmap(person, x, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawBitmap(ageBitmap, x+xOffsetPersonToPerson, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawBitmap(specBitmap, x+xOffsetPersonToPerson*2, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawBitmap(state, x+xOffsetPersonToPerson*3, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawText(text, x+xOffsetPersonToPerson*4, y+yOffsetText+10 + (p * yOffsetPerson), paintText);
                    if (dataManager.getEscaleras()>1){
                        String esc;
                        if (escalera==1){
                            esc = "escalera A";
                        } else {
                            esc = "escalera B";
                        }
                        paintText.setTextSize(30);
                        canvasPeople.drawText(esc, x+xOffsetPersonToPerson*4, y+yOffsetText +50 + (p * yOffsetPerson), paintText);
                    }
                    p++;
                }
            }

            state = BitmapFactory.decodeResource(this.getResources(),R.drawable.evacuado);
            for (int i = 0; i < evacTypes; i++) {
                if (i==1) {
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz4card);
                } else if (i==2) {
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz5card);
                } else if (i==3) {
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz6card);
                } else {
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personokcard);
                }

                int stateQuant = dataManager.readEvacQuant(altura, longitud, i, escalera);
                String age, spec;
                Bitmap ageBitmap, specBitmap;
                for (int j = 0; j < stateQuant; j++) {
                    age = dataManager.readEvacAge(altura, longitud, escalera, i, j);
                    spec = dataManager.readEvacSpec(altura, longitud, escalera, i, j);
                    switch (age) {
                        case "nino":
                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.nino);
                            break;
                        case "adulto":
                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.adulto);
                            break;
                        case "anciano":
                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.anciano);
                            break;
                        default:
                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.interrogante);
                            break;
                    }

                    switch (spec) {
                        case "bombero":
                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bombero);
                            break;
                        case "minusvalido":
                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.silla_ruedas);
                            break;
                        default:
                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.interrogante);
                            break;
                    }

                    canvasPeople.drawBitmap(borde, x, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawBitmap(person, x, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawBitmap(ageBitmap, x + xOffsetPersonToPerson, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawBitmap(specBitmap, x + xOffsetPersonToPerson * 2, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawBitmap(state, x + xOffsetPersonToPerson * 3, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawText(text, x + xOffsetPersonToPerson * 4, y + yOffsetText + 10 + (p * yOffsetPerson), paintText);
                    if (dataManager.getEscaleras()>1){
                        String esc;
                        if (escalera==1){
                            esc = "escalera A";
                        } else {
                            esc = "escalera B";
                        }
                        canvasPeople.drawText(esc, x+xOffsetPersonToPerson*4, y+yOffsetText +50 + (p * yOffsetPerson), paintText);
                    }
                    p++;
                }
            }

            state = BitmapFactory.decodeResource(this.getResources(),R.drawable.hospitalizado);
            for (int i = 0; i < hospTypes; i++) {
                if (i==1) {
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz4card);
                } else if (i==2) {
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz5card);
                } else if (i==3) {
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz6card);
                } else {
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personokcard);
                }


                int stateQuant = dataManager.readHospQuant(altura, longitud, i, escalera);
                String age, spec;
                Bitmap ageBitmap, specBitmap;
                for (int j = 0; j < stateQuant; j++) {
                    age = dataManager.readHospAge(altura, longitud, escalera, i, j);
                    spec = dataManager.readHospSpec(altura, longitud, escalera, i, j);
                    switch (age) {
                        case "nino":
                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.nino);
                            break;
                        case "adulto":
                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.adulto);
                            break;
                        case "anciano":
                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.anciano);
                            break;
                        default:
                            ageBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.interrogante);
                            break;
                    }

                    switch (spec) {
                        case "bombero":
                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bombero);
                            break;
                        case "minusvalido":
                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.silla_ruedas);
                            break;
                        default:
                            specBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.interrogante);
                            break;
                    }

                    canvasPeople.drawBitmap(borde, x, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawBitmap(person, x, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawBitmap(ageBitmap, x + xOffsetPersonToPerson, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawBitmap(specBitmap, x + xOffsetPersonToPerson * 2, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawBitmap(state, x + xOffsetPersonToPerson * 3, y + (p * yOffsetPerson), paint);
                    canvasPeople.drawText(text, x + xOffsetPersonToPerson * 4, y + yOffsetText + 10 + (p * yOffsetPerson), paintText);
                    if (dataManager.getEscaleras()>1){
                        String esc;
                        if (escalera==1){
                            esc = "escalera A";
                        } else {
                            esc = "escalera B";
                        }
                        canvasPeople.drawText(esc, x+xOffsetPersonToPerson*4, y+yOffsetText +30 + (p * yOffsetPerson), paintText);
                    }
                    p++;
                }
            }
        }
        view.invalidate();
    }

    private void drawEvacPeopleInArea(){
        Bitmap pers = BitmapFactory.decodeResource(this.getResources(), R.drawable.person);
        canvas.drawBitmap(pers, xOffsetPerson*10 , yOffsetMini, paint);
        Paint p = new Paint();
        p.setTextSize(180);
        if (evacuados) {
            canvas.drawText(String.valueOf(dataManager.getEvacuados()),xOffsetText,yOffsetText*2,p);
        } else if (hospital){
            canvas.drawText(String.valueOf(dataManager.getHospitalizados()),xOffsetText,yOffsetText*2,p);
        }
    }

    private void drawPeopleInApartment(int planta, int puerta){

        Bitmap person;
        int cant;
        reDrawApartment(planta, puerta);

        for (int i = 0; i<dataManager.confInApartment(planta,puerta, escalera); i++) {
            cant = dataManager.readConfQuant(planta, puerta, i, escalera);

            if (i==1) {
                person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz4card);
                paintText.setColor(Color.rgb(206,137,26));
            } else if (i==2) {
                person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz5card);
                paintText.setColor(Color.rgb(138,16,16));
            } else if (i==3) {
                person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz6card);
                paintText.setColor(Color.rgb(73,73,73));
            } else {
                person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personokcard);
                paintText.setColor(Color.rgb(24,109,18));
            }
            if (cant>0) {
                canvas.drawBitmap(person, xOffsetPerson + (i * xOffsetPersonToPerson), yOffsetPerson, paint);
                if (cant > 1)
                    canvas.drawText(Integer.toString(cant), xOffsetPerson + 5 + (i * xOffsetPersonToPerson), yOffsetPerson + 50, paintText);
            }
        }

        int personQ[] = new int[4];
        personQ[0] = 0;
        personQ[1] = 0;
        personQ[2] = 0;
        personQ[3] = 0;
        int cantE, cantH;
        for (int i=0;i<dataManager.evacInApartment(planta,puerta, escalera);i++) {
            cantE = dataManager.readEvacQuant(planta, puerta, i, escalera);
            cantH = dataManager.readHospQuant(planta, puerta, i, escalera);
            if (i==1) {
                personQ[1] += cantE;
                personQ[1] += cantH;
            } else if (i==2) {
                personQ[2] += cantE;
                personQ[2] += cantH;
            } else if (i==3) {
                personQ[3] += cantE;
                personQ[3] += cantH;
            } else {
                personQ[0] += cantE;
                personQ[0] += cantH;
            }
        }

        int offsetTimes = 0;
        for (int i=0;i<personQ.length;i++){
            if (personQ[i]>0) {
                if (i==0){
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personokcardmini);
                    paintText.setColor(Color.rgb(24,109,18));
                } else if (i==1){
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz4cardmini);
                    paintText.setColor(Color.rgb(206,137,26));
                } else if (i==2){
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz5cardmini);
                    paintText.setColor(Color.rgb(138,16,16));
                } else{
                    person = BitmapFactory.decodeResource(this.getResources(), R.drawable.personz6cardmini);
                    paintText.setColor(Color.rgb(73,73,73));
                }
                canvas.drawBitmap(person, xOffsetMini + (offsetTimes * xOffsetMiniToPerson), yOffsetMini, paint);
                canvas.drawText(Integer.toString(personQ[i]), xOffsetMini - 30 + (offsetTimes * xOffsetMiniToPerson), yOffsetMini + 90, paintText);
                offsetTimes++;
            }
        }
        imageView.invalidate();
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void dropPerson(String personType,int quant){
        dataManager.addConfPerson(altura,longitud,personType,quant, escalera);
        drawPeopleInApartment(altura,longitud);
        drawPeople(peopleView);
    }

    private void reDrawApartment(int planta, int puerta) {
        Bitmap blank;
        String im = dataManager.readImage(planta,puerta, escalera);
        String text = dataManager.readText(planta,puerta, escalera);
        String estado = dataManager.readState(planta,puerta, escalera);
        Paint p = paint;

        if (estado.contains("afectado")){
            im = im.concat("_afectado");
        } else if (estado.contains("origen")){
            im = im.concat("_origen");
        }
        if (estado.contains("estructural")){
            im = im.concat("_estructural");
        }

        int id = this.getResources().getIdentifier(im, "drawable", this.getPackageName());
        Bitmap bm = BitmapFactory.decodeResource(this.getResources(), id);
        blank = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
        if (estado.contains("no_existe")){
            im = "no_existe";
            id = this.getResources().getIdentifier(im, "drawable", this.getPackageName());
            bm = BitmapFactory.decodeResource(this.getResources(), id);
        }
        canvas.drawBitmap(blank, 0, 0, p);

        if (estado.contains("humo")){
            p = new Paint();
            ColorFilter filter = new LightingColorFilter(Color.LTGRAY, 1);
            p.setColorFilter(filter);
        }
        canvas.drawBitmap(bm, 0, 0, p);

        if (!text.isEmpty())
            paintText.setColor(Color.rgb(73,73,73));
        if (text.contains("Tejado")) {
            canvas.drawText(text, xOffsetText, yOffsetText * 2, paintText);
        } else {
            canvas.drawText(text, xOffsetText, yOffsetText, paintText);
        }
    }

    private void setOffsets(){
        Bitmap bm = BitmapFactory.decodeResource(thisActivity.getResources(),R.drawable.personokcard);
        int width = bm.getWidth();
        int height = bm.getHeight();

        xOffsetText = width/3;
        yOffsetText = height/2;

        xOffsetPerson = width/7;
        yOffsetPerson = (int)(height*1.1);
        xOffsetPersonToPerson = (int)(width*1.05);

        xOffsetMini = (int)(width*1.65);
        yOffsetMini = height/4;
        xOffsetMiniToPerson = (int)(width*0.75);
    }

    private void reDrawEvac(){
        ImageView evacView = findViewById(R.id.evacView);
        Bitmap evac = BitmapFactory.decodeResource(this.getResources(), R.drawable.evacuados);
        int w = evac.getWidth();
        int h = evac.getHeight();
        Bitmap bm = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
        evacView.setImageBitmap(bm);
        Canvas canv = new Canvas(bm);
        Bitmap pers = BitmapFactory.decodeResource(this.getResources(), R.drawable.person);
        canv.drawBitmap(evac, 0 , 0, paint);
        canv.drawBitmap(pers, xOffsetPerson*10 , yOffsetMini, paint);
        Paint p = new Paint();
        p.setTextSize(180);
        canv.drawText(String.valueOf(dataManager.getEvacuados()),xOffsetText,yOffsetText*2,p);
        evacView.invalidate();
    }

    private void reDrawHosp(){
        ImageView hospView = findViewById(R.id.hospView);
        Bitmap hosp = BitmapFactory.decodeResource(this.getResources(), R.drawable.hospital);
        int w = hosp.getWidth();
        int h = hosp.getHeight();
        Bitmap bm = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
        hospView.setImageBitmap(bm);
        Canvas canv = new Canvas(bm);
        Bitmap pers = BitmapFactory.decodeResource(this.getResources(), R.drawable.person);
        canv.drawBitmap(hosp, 0 , 0, paint);
        canv.drawBitmap(pers, xOffsetPerson*10 , yOffsetMini, paint);
        Paint p = new Paint();
        p.setTextSize(180);
        canv.drawText(String.valueOf(dataManager.getHospitalizados()),xOffsetText,yOffsetText*2,p);
        hospView.invalidate();
    }

    private int[] findPerson(int index){
        int quant;
        int state = 0;
        int type;
        boolean found = false;
        int[] personCoords;
        personCoords = new int[3];
        while (!found && state<4) {
            type = 0;
            while (!found && type<4){
                if (state==0) {
                    quant = dataManager.readConfQuant(altura, longitud, type, escalera);
                } else if (state==1){
                    quant = dataManager.readEvacQuant(altura, longitud, type, escalera);
                } else {
                    quant = dataManager.readHospQuant(altura, longitud, type, escalera);
                }
                if (index < quant) {
                    personCoords[0] = state;
                    personCoords[1] = type;
                    personCoords[2] = index;
                    found = true;
                } else {
                    index -= quant;
                    type++;
                }
            }
            state++;
        }

        return  personCoords;
    }

    private int[] findEvacPerson(int index){
        int quant;
        int state = 1;
        int type;
        boolean found = false;
        int[] personCoords;
        personCoords = new int[5];
        int a;
        int l;
        int esc = 0;
        while (!found && esc<dataManager.getEscaleras()) {
            a = 0;
            while (!found && a < dataManager.getAlturaTotal()) {
                l = 0;
                while (!found && l < dataManager.getLongitudTotal()) {
                    type = 0;
                    while (!found && type < 4) {
                        quant = dataManager.readEvacQuant(a, l, type, esc+1);
                        if (index < quant) {
                            personCoords[0] = state;
                            personCoords[1] = type;
                            personCoords[2] = index;
                            personCoords[3] = a;
                            personCoords[4] = l;
                            found = true;
                            escalera = esc+1;
                        } else {
                            index -= quant;
                            type++;
                        }
                    }
                    l++;
                }
                a++;
            }
            esc++;
        }
        return  personCoords;
    }

    private int[] findHospPerson(int index){
        int quant;
        int state = 2;
        int type;
        boolean found = false;
        int[] personCoords;
        personCoords = new int[5];
        int a;
        int l;
        int esc = 0;
        while (!found && esc<dataManager.getEscaleras()) {
            a= 0;
            while (!found && a<dataManager.getAlturaTotal()) {
                l = 0;
                while (!found && l<dataManager.getLongitudTotal()) {
                    type = 0;
                    while (!found && type<4) {
                        quant = dataManager.readHospQuant(a,l,type, esc+1);
                        if (index < quant){
                            personCoords[0] = state;
                            personCoords[1] = type;
                            personCoords[2] = index;
                            personCoords[3] = a;
                            personCoords[4] = l;
                            found = true;
                            escalera = esc+1;
                        } else {
                            index -= quant;
                            type++;
                        }
                    }
                    l++;
                }
                a++;
            }
            esc++;
        }
        return  personCoords;
    }

    private String newAge(String prevAge){
        String newAge;
        switch(prevAge){
            case "nino":
                newAge = "adulto";
                break;
            case "adulto":
                newAge = "anciano";
                break;
            case "anciano":
                newAge = "desconocida";
                break;
            default:
                newAge = "nino";
                break;
        }
        return newAge;
    }

    private String newSpecial(String prevSpec){
        String newSpec;
        switch (prevSpec){
            case "minusvalido":
                newSpec = "bombero";
                break;
            case "bombero":
                newSpec = "desconocido";
                break;
            default:
                newSpec = "minusvalido";
        }
        return newSpec;
    }

    private void modHospitalText(final int[] coords){

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(thisActivity);
        alertDialog.setTitle("HOSPITAL");
        final EditText hosp = new EditText(thisActivity);
        if (hospital){
            hosp.setText(dataManager.readHospitalName(coords[3],coords[4], escalera,coords[1], coords[2]));
        } else {
            hosp.setText(dataManager.readHospitalName(altura,longitud, escalera,coords[1],coords[2]));
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
        hosp.setLayoutParams(lp);
        alertDialog.setView(hosp);

        alertDialog.setPositiveButton("Guardar", new DialogInterface.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            public void onClick(DialogInterface dialog, int which) {
                if (hospital){
                    dataManager.editHospital(coords[3],coords[4], escalera,coords[1],coords[2],hosp.getText().toString());
                } else {
                    dataManager.editHospital(altura,longitud, escalera,coords[1],coords[2],hosp.getText().toString());
                    reDrawHosp();
                }
                drawPeople(peopleView);
            }
        });
        alertDialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alertDialog.show();
    }
}