package kz.istt.locust;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Locale;
import java.util.UUID;

import tools.LatLonListener;

public class LatLonAdapter extends ArrayAdapter<LatLon> {

    private LayoutInflater inflater;
    private int layout;
    public ArrayList<LatLon> latlonList;
    LatLonListener latlonListener=null;
    ListView productList=null;
    View.OnClickListener clc=null;
    private boolean disabled=false;

    LatLonAdapter(Context context, int resource, ArrayList<LatLon> latlon, LatLonListener latlonListener, ListView productList,boolean disabled) {
        super(context, resource, latlon);
        this.latlonList = latlon;
        this.layout = resource;
        this.inflater = LayoutInflater.from(context);
        this.latlonListener = latlonListener;
        this.disabled = disabled;
        //android:enabled="false"
        //android:focusable="false"
    }

    @Override
    public int getCount(){
        return latlonList.size();
    }

    /*public void addItem(LatLon item) {
        this.latlonList.add(item);
        this.notifyDataSetChanged();
    }*/

    /*@Override
    public void notifyDataSetChanged() {
        //ViewGroup.LayoutParams par = productList.getLayoutParams();
        //RelativeLayout.LayoutParams mParam = new RelativeLayout.LayoutParams(par.width,par.height+100);
        //productList.setLayoutParams(mParam);
    }*/

    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder;
        if(convertView==null){
            convertView = inflater.inflate(this.layout, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final LatLon latlon = latlonList.get(position);

        DecimalFormatSymbols custom = new DecimalFormatSymbols(new Locale("en", "US"));
        custom.setDecimalSeparator('.');
        DecimalFormat df = new DecimalFormat("0.0000000");
        df.setDecimalFormatSymbols(custom);

        if(viewHolder.edtLat!=null) {
            if (latlon.lat == 0) viewHolder.edtLat.setText("");
            else viewHolder.edtLat.setText(df.format(latlon.lat));
        }
        if(viewHolder.edtLon!=null) {
            if (latlon.lon == 0) viewHolder.edtLon.setText("");
            else viewHolder.edtLon.setText(df.format(latlon.lon));
        }
        if(viewHolder.btnDelGPS!=null) {
            viewHolder.btnDelGPS.setOnClickListener(new OnClickListenerIDDEL(position, this));
        }
        if(viewHolder.btnGetGPS!=null) {
            viewHolder.btnGetGPS.setOnClickListener(new OnClickListenerIDADD(position, this));
        }

        if(disabled) {
            viewHolder.btnGetGPS.setEnabled(false);
            viewHolder.btnGetGPS.setFocusable(false);

            viewHolder.btnDelGPS.setEnabled(false);
            viewHolder.btnDelGPS.setFocusable(false);
        }

        return convertView;
    }

    private class ViewHolder {
        final com.google.android.material.button.MaterialButton btnGetGPS, btnDelGPS;
        final com.google.android.material.textfield.TextInputEditText edtLat, edtLon;
        ViewHolder(View view){
            btnGetGPS = view.findViewById(R.id.btnGetGPS);
            btnDelGPS = view.findViewById(R.id.btnDelGPS);
            edtLat = view.findViewById(R.id.edtLat);
            edtLon = view.findViewById(R.id.edtLon);
        }
    }

    //To add an element to a list
    class OnClickListenerIDADD implements View.OnClickListener{
        int position;
        LatLonAdapter adapter;
        OnClickListenerIDADD(int position,LatLonAdapter adapter){
            this.position=position;
            this.adapter=adapter;
        }
        @Override
        public void onClick(View view) {
            LatLon latlon = adapter.latlonList.get(position);
            latlon.lat=adapter.latlonListener.getLat();
            latlon.lon=adapter.latlonListener.getLon();
            if(adapter.latlonList.get(adapter.latlonList.size()-1).lat!=0 && adapter.latlonList.get(adapter.latlonList.size()-1).lon!=0){
                adapter.latlonList.add(new LatLon(UUID.randomUUID().toString(),0, 0));
                if(adapter.clc!=null){
                    adapter.clc.onClick(null);
                }
            }
            adapter.notifyDataSetChanged();
        }
    }
    //To remove an element from a list
    class OnClickListenerIDDEL implements View.OnClickListener{
        int position;
        LatLonAdapter adapter;
        OnClickListenerIDDEL(int position,LatLonAdapter adapter){
            this.position=position;
            this.adapter=adapter;
        }
        @Override
        public void onClick(View view) {
            LatLon latlon = adapter.latlonList.get(position);
            latlon.lat=0;
            latlon.lon=0;
            if(latlonList.size()>1 && position!=latlonList.size()-1) {
                latlonList.remove(position);
                if(adapter.clc!=null){
                    adapter.clc.onClick(null);
                }
            }
            adapter.notifyDataSetChanged();
        }
    }
}
