package com.dbs.dbsec.activity.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.TextView;

import com.dbs.dbsec.BuildConfig;
import com.dbs.dbsec.R;
import com.dbs.dbsec.activity.ConsentActivity;
import com.dbs.dbsec.activity.adapter.ConsentListAdapter;
import com.dbs.dbsec.httpTask.CallBack;
import com.dbs.dbsec.httpTask.ParseJsonData;
import com.dbs.dbsec.model.ConsentFormListVO;
import com.dbs.dbsec.util.Common;
import com.dbs.dbsec.util.ConsentConfig;

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

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;

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);

    getUserFormSetList();
    return view;
  }

  /**
   * 동의서 즐겨찾기 목록을 가져온다
   * @author jksong
   * @since 2020-01-28 오후 5:02
   **/
  public void getUserFormSetList() {
    CallBack callBack = new CallBack() {
      @Override
      public void result(final String result) {
        Log.e(TAG, "getUserFormSet -- > " + result);
        mActivity.runOnUiThread(new Runnable() {
          @Override
          public void run() {
            instance.dismissProgress();
            ArrayList<HashMap<String, String>> arrayList = Common.parseJSON(mContext, result);
            String code = arrayList.get(0).get("code");

            if (!code.equals("00")) {
              stop();
            } else {
              tvEmptyMarker.setVisibility(View.GONE);
              ArrayList<ConsentFormListVO> consentArr = ParseJsonData.getInstance(mContext).getConsentFormList(arrayList);
              setConsentSetAdapter(consentArr);
            }
          }
        });
      }

      @Override
      public void stop() {
        tvEmptyMarker.setVisibility(View.VISIBLE);
      }

      @Override
      public void error() {

      }
    };

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

    if (Common.checkNetwork(mContext) == true) {
      instance.showProgress();
      Common.callHttp(mContext, ConsentConfig.GET_USER_FORM_SET, mData, callBack);
    } else {
      Common.networkErrorAlert(mActivity);
    }
  }

  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_USER_FORM_SET);
      markerRecyclerView.setAdapter(consentListAdapter);
    } else {
      consentListAdapter.arrayList = arrayList;
      consentListAdapter.notifyDataSetChanged();
    }
  }


}


