/*
 * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
 */
package kr.co.zumo.app.lifeplus.view.fragment.signup;

import android.util.Log;
import android.view.View;
import android.widget.CheckBox;

import java.util.ArrayList;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.util.ResourceUtil;
import kr.co.zumo.app.lifeplus.view.CheckBoxAllDriver;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.bean.SignUpAgreeItemBean;
import kr.co.zumo.app.lifeplus.view.presenter.signup.SignUpItem;

/**
 * SignUpAgreeHolder
 * <pre>
 * </pre>
 *
 * @author 하세미
 * @version 1.0
 * @history 하세미   [2018-09-12]   [최초 작성]
 * @since 2018-09-12
 */
public class SignUpAgreeHolder extends BaseViewHolder {

  ISignUpHolderEventListener listener;
  ArrayList<SignUpAgreeItemBean> items;
  CheckBox checkBoxAll;
  CheckBoxAllDriver checkBoxAllDriver;
  int len;

  public SignUpAgreeHolder(View itemView, ArrayList<SignUpAgreeItemBean> items) {
    super(itemView);
    this.items = items;
  }

  @Override
  void draw(SignUpItem item) {
    listener = item.getEventListener();

    checkBoxAll = itemView.findViewById(R.id.agree_check_all);

    checkBoxAllDriver = new CheckBoxAllDriver(checkBoxAll);

    checkBoxAll.setText(R.string.all_agree);
    checkBoxAll.setOnClickListener(v -> {
      checkBoxAllDriver.applyCheckAll(checkBoxAll.isChecked());
      checkAll(checkBoxAll.isChecked());
      notifyAgreeChanged();
    });


    len = items.size();
    CheckBox checkBox;
    View view; //maybe image view
    for (int i = 0; i < len; ++i) {
      checkBox = getCheckBoxByIndex(i);
      checkBoxAllDriver.addChildBox(checkBox);

      SignUpAgreeItemBean bean = items.get(i);
      checkBox.setText(new StringBuilder().append(bean.isRequired() ? ResourceUtil.getString(R.string.agree_required) : ResourceUtil.getString(R.string.agree_option)).append(bean.getText()).toString());
      checkBox.setChecked(bean.isChecked());
      checkBox.setEnabled(bean.isEnabled());
      checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
        bean.setChecked(isChecked);
        checkBoxAllDriver.check();
        notifyAgreeChanged();
      });
      int index = i;
      view = itemView.findViewById(ResourceUtil.getId("agree_detail_button" + i));
      view.setOnClickListener(v -> {
        notifyDetailPressed(index, bean.toJson());
      });
    }

  }

  @Override
  public void setDisabled() {
    checkBoxAll.setEnabled(false);
    SignUpAgreeItemBean bean;
    CheckBox checkBox;
    for (int i = 0; i < len; ++i) {
      bean = items.get(i);
      bean.setEnabled(false);
      checkBox = getCheckBoxByIndex(i);
      checkBox.setEnabled(false);
    }
  }

  @Override
  public void setEnabled() {

  }

  private CheckBox getCheckBoxByIndex(int index) {
    return itemView.findViewById(ResourceUtil.getId("agree_check" + index));
  }

  private void notifyAgreeChanged() {
    listener.onSignUpHolderEvent(this, Event.HOLDER_AGREE_CHANGED);
  }

  private void notifyDetailPressed(int index, String data) {
    Log.i("APP# SignUpAgreeHolder | draw", "| index: " + index + ", data: " + data);
    listener.onSignUpHolderEvent(this, Event.HOLDER_DETAIL, index, data);
  }

  private void checkAll(boolean isChecked) {
    SignUpAgreeItemBean bean;
    for (int i = 0; i < len; ++i) {
      bean = items.get(i);
      bean.setChecked(isChecked);
    }
  }


}
