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 androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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.TextView;

import com.dbs.mplus.knuh.BuildConfig;
import com.dbs.mplus.knuh.R;
import com.dbs.mplus.knuh.activity.ConsentActivity;
import com.dbs.mplus.knuh.adapter.ConsentListAdapter;
import com.dbs.mplus.knuh.httpTask.CallBack;
import com.dbs.mplus.knuh.httpTask.SoapParser;
import com.dbs.mplus.knuh.model.ConsentFormListVO;
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 MarkerFragment extends Fragment {

  private static final String TAG = "MarkerFragment";
  private AppCompatActivity mActivity;
  private Context mContext;
  private ConsentActivity instance;
  private ConsentListAdapter consentListAdapter;

  private LinearLayoutManager linearLayoutManager;
  private RecyclerView markerRecyclerView;
  private TextView tvEmptyMarker;


  public MarkerFragment(AppCompatActivity mActivity, Context mContext) {
    this.mActivity = mActivity;
    this.mContext  = mContext;
    this.instance  = (ConsentActivity) mContext;
  }

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

  }

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

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

    markerRecyclerView = (RecyclerView) view.findViewById(R.id.markerRecyclerView);
    tvEmptyMarker      = (TextView) view.findViewById(R.id.tvEmptyMarker);
    
    getConsentSetList();
    return view;
  }

  /**
  * 동의서 즐겨찾기 목록을 가져온다
  * @author jksong
  * @since 2020-01-28 오후 5:02
  **/
  public void getConsentSetList() {
    instance.showProgress();
    CallBack callBack = new CallBack() {
      @Override
      public void result(final SoapObject result) {

        mActivity.runOnUiThread(new Runnable() {
          @Override
          public void run() {
            Log.e(TAG, "getConsentSetList result -- > " + result);
            if (result == null || result.toString().indexOf("null") > 0 || result.getPropertyCount() == 0) {
              stop();
              tvEmptyMarker.setVisibility(View.VISIBLE);
            } else {
              stop();
              tvEmptyMarker.setVisibility(View.GONE);
              ArrayList<ConsentFormListVO> arrayList = SoapParser.getConsentList(result);
              setConsentSetAdapter(arrayList);
            }
          }
        });

      }

      @Override
      public void stop() {
        instance.dismissProgress();
      }

      @Override
      public void error() {

      }
    };

    HashMap<String, String> mData = new HashMap<String, String>();
    mData.put("userId", Util.getStringPreference(mContext, "userInfo", "userId"));
//    mData.put("userId", "123123");
    mData.put("instCd", BuildConfig.INST_CD);

    if (Util.checkNetwork(mContext) == true) {
      Util.callHttp(mContext, ConsentConfig.HOST_HOSPITAL, ConsentConfig.GET_CONSENT_SET_LIST, mData, callBack);
    } else {
      instance.dismissProgress();
      Util.networkErrorAlert(mActivity);
    }
  }

  /**
  * 즐겨찾기 목록 데이터를 어댑터에 연결해준다
  * @author jksong
  * @since 2020-01-28 오후 5:03
  **/
  private void setConsentSetAdapter(final ArrayList<ConsentFormListVO> arrayList) {

    if (consentListAdapter == null) {
      linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
      markerRecyclerView.setLayoutManager(linearLayoutManager);
      consentListAdapter = new ConsentListAdapter(mActivity, mContext, arrayList, ConsentConfig.GET_CONSENT_SET_LIST);
      markerRecyclerView.setAdapter(consentListAdapter);
    } else {
      consentListAdapter.arrayList = arrayList;
      consentListAdapter.notifyDataSetChanged();
    }
  }

}
