package kr.co.zumo.app.lifeplus.view.fragment.signup;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import java.util.List;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.bean.api.PolicyBean;
import kr.co.zumo.app.lifeplus.model.SignUpModel;
import kr.co.zumo.app.lifeplus.util.ResourceUtil;
import kr.co.zumo.app.lifeplus.util.StringUtil;
import kr.co.zumo.app.lifeplus.view.CheckBoxAllDriver;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.fragment.FragmentBase;
import kr.co.zumo.app.lifeplus.view.presenter.signup.ISignUpAgreeView;
import kr.co.zumo.app.lifeplus.view.presenter.signup.SignUpAgreePresenter;


/**
 * SignUpAgreeFragment
 * <pre>
 * </pre>
 *
 * @author 하세미
 * @version 1.0
 * @history 하세미   [2018-09-21]   [최초 작성]
 * @since 2018-09-21
 */
public class SignUpAgreeFragment extends FragmentBase<SignUpAgreePresenter> implements ISignUpAgreeView {

  private List<PolicyBean> items;
  private CheckBox checkBoxAll;
  private CheckBoxAllDriver checkBoxAllDriver;
  private int len;
  private View buttonAgree;

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

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_sign_up_agree, container, false);
  }

  @Override
  protected void onAfterActivityCreated(Bundle savedInstanceState) {

    View parent = getView();
    checkBoxAll = parent.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();
    });

    buttonAgree = parent.findViewById(R.id.button_sign_up_agree);

    presenter.onEvent(new Event.Builder(Event.INIT).build());

  }


  @Override
  protected SignUpAgreePresenter definePresenter() {
    return new SignUpAgreePresenter(getModel(SignUpModel.class), this);
  }


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

  private void notifyAgreeChanged() {
    presenter.onEvent(new Event.Builder(Event.CHANGED).build());
  }

  private void notifyDetailPressed(int index, String data) {
    Log.i("APP# SignUpAgreeHolder | draw", "| index: " + index + ", data: " + data);
    presenter.onEvent(new Event.Builder(Event.DETAIL).integer(index).json(data).build());

  }

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

  @Override
  public void init(List<PolicyBean> agreeItemBeans) {

    this.items = agreeItemBeans;
    View parent = getView();

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

      PolicyBean bean = items.get(i);
      String title = bean.getTitle();
      title = title.replace(ResourceUtil.getString(R.string.agree_required), "");
      title = title.replace(ResourceUtil.getString(R.string.agree_option), "");
      title = title.trim();
      checkBox.setText(StringUtil.fromHtml(
        new StringBuilder().append("<b>").append(bean.isMandatory() ? ResourceUtil.getString(R.string.agree_required) : ResourceUtil.getString(R.string.agree_option))
          .append("</b> ").append(title).toString()));
      checkBox.setChecked(bean.isChecked());
      checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
        bean.setChecked(isChecked);
        checkBoxAllDriver.check();
        notifyAgreeChanged();
      });
//      checkBox.setTypeface(ResourcesCompat.getFont(getContext(), R.font.font1));
      int index = i;
      view = parent.findViewById(ResourceUtil.getId("agree_detail_button" + i));
      view.setOnClickListener(v -> {
        notifyDetailPressed(index, bean.toJson());
      });
    }

    setAgreeButtonEnabled(false);
    buttonAgree.setOnClickListener(v -> {
      presenter.onEvent(new Event.Builder(Event.SUCCESS).build());
    });
//    checkBoxAll.setTypeface(ResourcesCompat.getFont(getContext(), R.font.font1));
  }

  @Override
  public void setAgreeButtonEnabled(boolean isEnabled) {
    buttonAgree.setEnabled(isEnabled);
  }
}
