package com.airson.alive.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.airson.alive.R;
import com.airson.alive.activity.base.BaseActivity;
import com.airson.alive.adapter.HomeAdapter;
import com.airson.alive.adapter.MedicalIDProfilesAdapter;
import com.airson.alive.interfaces.OnItemClickListener;
import com.airson.alive.model.MedicalIDsResponseModel;
import com.airson.alive.model.UserResponseModel;
import com.airson.alive.network.ApiClient;
import com.airson.alive.network.ApiInterface;
import com.airson.alive.preference.Preferences;
import com.airson.alive.utils.CommonUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MedicalIDProfilesActivity extends BaseActivity{

    @BindView(R.id.medicalid_recyclerview)
    RecyclerView recyclerView;

    ArrayList<MedicalIDsResponseModel.MedicalIDDataSetFields> arrayList = null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_medicalid_profiles);
        setUnBinder(ButterKnife.bind(this));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Medical ID");
        arrayList = new ArrayList<>();

        if (CommonUtils.isInternetOn(this, true)){
            showLoading();
            try {
                ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
                Call<MedicalIDsResponseModel> call = apiService.getMedicalIDsList(Preferences.getInstance().getAuthToken(this), Preferences.getInstance().getMobile(this));
                call.enqueue(new Callback<MedicalIDsResponseModel>() {
                    @Override
                    public void onResponse(Call<MedicalIDsResponseModel> call, Response<MedicalIDsResponseModel> response) {
                        hideLoading();
                        if(response.body()!=null){
                            if(response.body().isError()==false){
                                MedicalIDsResponseModel.MedicalIDDataSetFields dataSetFields = new MedicalIDsResponseModel.MedicalIDDataSetFields();
                                dataSetFields.setMid("0");
                                if(response.body().getMedicalArray().size()>0){
                                    response.body().getMedicalArray().add(0, dataSetFields);
                                    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(MedicalIDProfilesActivity.this, 2);
                                    recyclerView.setLayoutManager(mLayoutManager);
                                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                                    recyclerView.setAdapter(new MedicalIDProfilesAdapter(MedicalIDProfilesActivity.this, response.body().getMedicalArray()));

                                } else {
                                     arrayList.add(dataSetFields);
                                    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(MedicalIDProfilesActivity.this, 2);
                                    recyclerView.setLayoutManager(mLayoutManager);
                                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                                    recyclerView.setAdapter(new MedicalIDProfilesAdapter(MedicalIDProfilesActivity.this, arrayList));

                                }



                            } else {
                                showMessage(response.body().getMessage());
                            }
                        }


                    }

                    @Override
                    public void onFailure(Call<MedicalIDsResponseModel>call, Throwable t) {
                        // Log error here since request failed
                        hideLoading();
                        showMessage(t.getMessage());
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void setUp() {

    }
}
