package com.dbs.consent.demo.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Button;

import com.dbs.consent.demo.R;
import com.dbs.consent.demo.adapter.DepartmentAdapter;
import com.dbs.consent.demo.model.UserLoginDeptList;

import java.util.ArrayList;

public class DepartmentList extends AppCompatActivity {

  private static final String TAG = "DepartmentList";
  private AppCompatActivity mActivity;
  private Context mContext;

  private ArrayList<UserLoginDeptList> arrayList;

  private RecyclerView recyclerView;
  private Button btnSave;
  private Button btnCancel;

  private LinearLayoutManager layoutManager;

  private DepartmentAdapter departmentAdapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_department_list);

    //API26에서만 Translucent/Floating 으로 만든 투명한 Activity들은 화면 회전고정을 하지 못하함
//    try {
//      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//    } catch (IllegalStateException ignore) {
//    }

    init();
  }

  private void init() {
    mActivity = this;
    mContext  = this;
    // for test
//    int a = 0;

    arrayList = new ArrayList<UserLoginDeptList>();

    Intent intent = getIntent();
    arrayList = (ArrayList<UserLoginDeptList>) intent.getSerializableExtra("departmentList");

    setLayout();
  }

  private void setLayout() {
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    btnSave = (Button) findViewById(R.id.btnSave);
    btnCancel = (Button) findViewById(R.id.btnCancel);
    layoutManager = new LinearLayoutManager(mActivity, RecyclerView.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);

    Drawable saveBg = (Drawable) btnSave.getBackground();
    Drawable cancelBg = (Drawable) btnCancel.getBackground();

    saveBg.setColorFilter(ContextCompat.getColor(mContext, R.color.departmentBtnColor), PorterDuff.Mode.SRC_ATOP);
    cancelBg.setColorFilter(ContextCompat.getColor(mContext, R.color.departmentBtnColor), PorterDuff.Mode.SRC_ATOP);

    setAdapter();
  }

  private void setAdapter() {
    if (departmentAdapter == null) {
      departmentAdapter = new DepartmentAdapter(mActivity, arrayList);
      recyclerView.setAdapter(departmentAdapter);
    } else {
      departmentAdapter.arrayList = arrayList;
      departmentAdapter.notifyDataSetChanged();
    }
  }

}
