package com.dbs.mplus.knuh.fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.dbs.mplus.knuh.BuildConfig;
import com.dbs.mplus.knuh.R;
import com.dbs.mplus.knuh.adapter.CategoryAdapter;
import com.dbs.mplus.knuh.httpTask.CallBack;
import com.dbs.mplus.knuh.httpTask.SoapParser;
import com.dbs.mplus.knuh.model.CategoryVO;
import com.dbs.mplus.knuh.util.ConsentConfig;
import com.dbs.mplus.knuh.util.Util;

import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;

public class SearchFragment extends Fragment {

  private static final String TAG = "SearchFragment";

  private AppCompatActivity mActivity;
  private Context mContext;

  private CategoryAdapter categoryAdapter;

  private Spinner      searchDeptSpinner;
  private LinearLayout searchHospitalLayout;
  private LinearLayout searchDeptLayout;

  public SearchFragment() {
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mContext  = getActivity();
    mActivity = (AppCompatActivity) getActivity();
  }


  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = (View) inflater.inflate(R.layout.activity_search_fragment, container, false);

    searchHospitalLayout  = (LinearLayout) view.findViewById(R.id.searchHospitalLayout);
    searchDeptLayout      = (LinearLayout) view.findViewById(R.id.searchDeptLayout);
    searchDeptSpinner     = (Spinner) view.findViewById(R.id.searchDeptSpinner);

    getHospitalList(BuildConfig.INST_CD);

    return view;
  }

  private void getHospitalList(final String instCd) {
    CallBack callBack = new CallBack() {
      @Override
      public void result(SoapObject result) {
        Log.e(TAG, "RowListResult --> " + result);
        if (result == null || result.toString().indexOf("null") > 0) {
          stop();
        } else {
          ArrayList<CategoryVO> arrayList = SoapParser.getHospitalList(result);
          setSpinnerAdapter(arrayList);
        }
      }

      @Override
      public void stop() {

      }

      @Override
      public void error() {

      }
    };

    HashMap<String, String> mData = new HashMap<String, String>();
    mData.put("categoryId", instCd);
    Util.callHttp(mContext, ConsentConfig.HOST_CONSENT, ConsentConfig.GET_CONSENT_CATEGORYY, mData, callBack);
  }

  private void setSpinnerAdapter(final ArrayList arrayList) {
    mActivity.runOnUiThread(new Runnable() {
      @Override
      public void run() {
        categoryAdapter = new CategoryAdapter(mActivity, arrayList);
        searchDeptSpinner.setAdapter(categoryAdapter);

      }
    });
  }
}