package io.vehiclehistory.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import io.vehiclehistory.DateFormatter;
import io.vehiclehistory.R;
import io.vehiclehistory.activity.VehicleDataActivity;
import io.vehiclehistory.api.model.Vehicle;
import io.vehiclehistory.api.model.VehicleResponse;

/**
 * @author Piotr Makowski (<a href=\"mailto:Piotr.Makowski@allegrogroup.pl\">Piotr.Makowski@allegrogroup.pl</a>)
 */
public class VehicleInfoFragment extends Fragment {

    public static VehicleInfoFragment newInstance(VehicleResponse vehicleResponse) {
        VehicleInfoFragment fragment = new VehicleInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(VehicleDataActivity.EXTRA_VEHICLE_RESPONSE_KEY, vehicleResponse);
        fragment.setArguments(bundle);
        return fragment;
    }

    public VehicleInfoFragment() {
        //nop
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_vehicle_info, container, false);

        Bundle args = getArguments();
        VehicleResponse vehicleResponse = (VehicleResponse) args.getSerializable(
                VehicleDataActivity.EXTRA_VEHICLE_RESPONSE_KEY);

        bindDataToView(vehicleResponse.getVehicle(), rootView);

        return rootView;
    }

    private void bindDataToView(Vehicle vehicle, View rootView) {
        TextView carLabel = (TextView) rootView.findViewById(R.id.car_label);
        carLabel.setText(vehicle.getName().getMake() + " " + vehicle.getName().getModel());

        TextView producer = (TextView) rootView.findViewById(R.id.producer);
        producer.setText(vehicle.getName().getMake().toString());

        TextView model = (TextView) rootView.findViewById(R.id.model);
        model.setText(vehicle.getName().getModel());

        TextView year = (TextView) rootView.findViewById(R.id.year);
        year.setText(vehicle.getProduction() != null ? vehicle.getProduction().getYear() : "");

        TextView reg_number = (TextView) rootView.findViewById(R.id.reg_number);
        reg_number.setText(vehicle.getPlate().getValue());

        TextView vin = (TextView) rootView.findViewById(R.id.vin);
        vin.setText(vehicle.getVin());

        TextView first_registration = (TextView) rootView.findViewById(R.id.first_registration);
        first_registration.setText(new DateFormatter().formatDateFromApi(vehicle.getRegistration().getFirstAt()));

        TextView engine_capacity = (TextView) rootView.findViewById(R.id.engine_capacity);
        engine_capacity.setText(vehicle.getEngine().getCubicCapacity());

        TextView fuel_type = (TextView) rootView.findViewById(R.id.fuel_type);
        fuel_type.setText(vehicle.getEngine().getFuel().toString());

        TextView mileage = (TextView) rootView.findViewById(R.id.mileage);
        mileage.setText(vehicle.getMileage() != null ?
                vehicle.getMileage().getValue() : getActivity().getString(R.string.no_data));

        TextView insurance = (TextView) rootView.findViewById(R.id.insurance);
        insurance.setText(vehicle.getPolicy().getStatus().toString());

        TextView registration_status = (TextView) rootView.findViewById(R.id.registration_status);
        registration_status.setText(vehicle.getRegistration().getStatus().toString());

        TextView technical_examination = (TextView) rootView.findViewById(R.id.technical_examination);
        technical_examination.setText(vehicle.getInspection().getStatus().toString());

        TextView vehicle_stolen = (TextView) rootView.findViewById(R.id.vehicle_stolen);
        vehicle_stolen.setText(vehicle.getStolen() ?
                getActivity().getString(R.string.yes) : getActivity().getString(R.string.no));

        TextView vehicle_class = (TextView) rootView.findViewById(R.id.vehicle_class);
        vehicle_class.setText(vehicle.getType().getKind().getValueResource());

        TextView vehicle_type = (TextView) rootView.findViewById(R.id.vehicle_type);
        vehicle_type.setText(vehicle.getType().getType().getValueResource());
    }

}

@@ -2200,26 +2200,25 @@
 TextView reg
-_n
+N
 umber = (Tex
@@ -2275,18 +2275,17 @@
      reg
-_n
+N
 umber.se
@@ -2448,26 +2448,25 @@
 xtView first
-_r
+R
 egistration 
@@ -2535,26 +2535,25 @@
        first
-_r
+R
 egistration.
@@ -2660,26 +2660,25 @@
 tView engine
-_c
+C
 apacity = (T
@@ -2741,26 +2741,25 @@
       engine
-_c
+C
 apacity.setT
@@ -2825,18 +2825,17 @@
 iew fuel
-_t
+T
 ype = (T
@@ -2894,18 +2894,17 @@
     fuel
-_t
+T
 ype.setT
@@ -3352,26 +3352,25 @@
 registration
-_s
+S
 tatus = (Tex
@@ -3441,26 +3441,25 @@
 registration
-_s
+S
 tatus.setTex
@@ -3530,34 +3530,33 @@
 xtView technical
-_e
+E
 xamination = (Te
@@ -3623,34 +3623,33 @@
        technical
-_e
+E
 xamination.setTe
@@ -3721,26 +3721,25 @@
 View vehicle
-_s
+S
 tolen = (Tex
@@ -3800,26 +3800,25 @@
      vehicle
-_s
+S
 tolen.setTex
@@ -3957,26 +3957,25 @@
 View vehicle
-_c
+C
 lass = (Text
@@ -4034,26 +4034,25 @@
      vehicle
-_c
+C
 lass.setText
@@ -4126,18 +4126,17 @@
  vehicle
-_t
+T
 ype = (T
@@ -4201,18 +4201,17 @@
  vehicle
-_t
+T
 ype.setT

Use CamelCase.