package cl.smartcities.isci.transportinspector.busesAndEventsDetails;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import cl.smartcities.isci.transportinspector.R;
import cl.smartcities.isci.transportinspector.adapters.IncomingBusAdapter;
import cl.smartcities.isci.transportinspector.backend.BusStop;
import cl.smartcities.isci.transportinspector.backend.BusesInService;
import cl.smartcities.isci.transportinspector.drawables.LocationOnOffImageView;
import cl.smartcities.isci.transportinspector.model.gson.ServerSentBus;

/**
 * Created by Agustin on 7/8/2017.
 */

public class BusStopBusesDetailFragment extends Fragment implements IncomingBusAdapter.OnLocationVisibilityIconListener, IncomingBusAdapter.OnBusEventsClickedListener {
    private ArrayList<ServerSentBus> buses;
    private BusStop busStop;
    private IncomingBusAdapter adapter;

    private int totalServices;
    private int visibleServices;

    private LocationOnOffImageView visibilityIcon;

    // newInstance constructor for creating fragment with arguments
    public static BusStopBusesDetailFragment newInstance(ArrayList<ServerSentBus> buses, BusStop busStop) {
        BusStopBusesDetailFragment fragmentFirst = new BusStopBusesDetailFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList("buses", buses);
        args.putParcelable("busStop", busStop);
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }

    // Store instance variables based on arguments passed
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.buses = getArguments().getParcelableArrayList("buses");
        this.busStop = getArguments().getParcelable("busStop");
    }

    // Inflate the view for the fragment based on layout XML
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.detail_fragment_bus_layout, container, false);
        TextView busStopTitle = view.findViewById(R.id.bus_stop_title);
        busStopTitle.setText(busStop.getName());

        RecyclerView incomingBusesRecyclerView = view.findViewById(R.id.incoming_bus_recycler_view);

        visibilityIcon = view.findViewById(R.id.visibility_icon);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getContext());
        incomingBusesRecyclerView.setLayoutManager(layoutManager);

        ArrayList<BusesInService> services = getBusesInServices(buses);

        totalServices = services.size();
        visibleServices = 0;
        for (BusesInService service : services) {
            if (service.isVisible()) {
                visibleServices += 1;
            }
        }
        changeVisibilityIcon();

        adapter = new IncomingBusAdapter(services, this, this);
        incomingBusesRecyclerView.setAdapter(adapter);

        visibilityIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (visibilityIcon.isVisible()) {
                    visibilityIcon.setVisibility(LocationOnOffImageView.Visibility.OFF);
                    adapter.selectAllOff();
                    visibleServices = 0;
                } else {
                    visibilityIcon.setVisibility(LocationOnOffImageView.Visibility.ON);
                    adapter.selectAllOn();
                    visibleServices = totalServices;
                }
            }
        });

        return view;
    }

    private ArrayList<BusesInService> getBusesInServices(ArrayList<ServerSentBus> buses) {
        ArrayList<BusesInService> services = BusesInService.getServicesFromBuses(buses, busStop.getId());

        for (String route : busStop.getServices()) {
            BusesInService newService = new BusesInService(route, null);
            if (!services.contains(newService)) {
                services.add(newService);
            }
        }
        return services;
    }

    private void changeVisibilityIcon() {
        if (visibleServices == 0) {
            visibilityIcon.setVisibility(LocationOnOffImageView.Visibility.OFF);
        } else if (visibleServices == totalServices) {
            visibilityIcon.setVisibility(LocationOnOffImageView.Visibility.ON);
        } else {
            visibilityIcon.setVisibility(LocationOnOffImageView.Visibility.ON_OFF);
        }
    }

    public void changedData(ArrayList<ServerSentBus> buses) {
        if (busStop==null) {
            return;
        }
        adapter.changeItems(getBusesInServices(buses));
    }

    @Override
    public void onServiceVisibilityChange(boolean changeToVisible) {
        visibleServices = visibleServices + (changeToVisible ? 1 : -1);
        changeVisibilityIcon();
    }

    @Override
    public void onBusEventsClicked(ServerSentBus bus) {
        Intent intent= new Intent(getContext(), BusReportDetailsActivity.class);

        Bundle bundle = new Bundle();
        bundle.putParcelable(DetailsActivity.BUSES, bus);
        bundle.putParcelableArrayList(DetailsActivity.EVENTS, bus.getEvents());
        bundle.putParcelable(DetailsActivity.BUS_STOP, busStop);

        intent.putExtras(bundle);
        startActivity(intent);
    }
}
