/*
 * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
 */
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.support.annotation.StringRes;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import io.reactivex.disposables.Disposable;
import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.model.SignUpModel;
import kr.co.zumo.app.lifeplus.util.StringUtil;
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.ISignUpPinView;
import kr.co.zumo.app.lifeplus.view.presenter.signup.SignUpPinPresenter;

/**
 * SignUpPinFragment
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 28.]   [최초 작성]
 * @since 2018. 9. 28.
 */
public class SignUpPinFragment extends FragmentBase<SignUpPinPresenter> implements ISignUpPinView {

  private ArrayList<Integer> numberList;
  private ArrayList<Button> buttonList;
  private ArrayList<ImageView> imageList;
  private String password;
  private String guideString = "";
  @StringRes
  private int titleStringId = R.string.empty_string;

  private TextView textGuide;
  private TextView textTitle;
  private TextView textReset;
  private Disposable disposable;
  private Button buttonShuffle;
  private ImageButton buttonDelete;

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

  @Override
  protected void onAfterActivityCreated(Bundle savedInstanceState) {

    password = "";

    View view = getView();

    buttonShuffle = view.findViewById(R.id.button_shuffle);
    buttonDelete = view.findViewById(R.id.button_delete);
    textGuide = view.findViewById(R.id.text_guide);
    textTitle = view.findViewById(R.id.text_title);
    textReset = view.findViewById(R.id.button_sign_up_pin_code_reset);

    if (StringUtil.isFull(guideString)) {
      textGuide.setText(guideString);
    }

    if (R.string.empty_string != titleStringId) {
      textTitle.setText(titleStringId);
    }

    numberList = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
    buttonList = createButtonListFromView(view);
    imageList = createImageListFromView(view);

    buttonShuffle.setOnClickListener(v -> shuffleKeyboard());

    buttonDelete.setOnClickListener(v -> doSomeAfterClickDeleteButton());

    int size = buttonList.size();
    for (int i = 0; i < size; ++i) {
      buttonList.get(i).setOnClickListener(v -> doSomeAfterClickNumberButton(v));
    }

    shuffleKeyboard();

    //pinDriver.onPinStart();
    presenter.onEvent(new Event.Builder(Event.INIT).build());
  }

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


  private ArrayList<Button> createButtonListFromView(View view) {
    ArrayList<Button> list = new ArrayList<Button>();
    list.add((Button) view.findViewById(R.id.button_0));
    list.add((Button) view.findViewById(R.id.button_1));
    list.add((Button) view.findViewById(R.id.button_2));
    list.add((Button) view.findViewById(R.id.button_3));
    list.add((Button) view.findViewById(R.id.button_4));
    list.add((Button) view.findViewById(R.id.button_5));
    list.add((Button) view.findViewById(R.id.button_6));
    list.add((Button) view.findViewById(R.id.button_7));
    list.add((Button) view.findViewById(R.id.button_8));
    list.add((Button) view.findViewById(R.id.button_9));

    return list;
  }

  private ArrayList<ImageView> createImageListFromView(View view) {
    ArrayList<ImageView> list = new ArrayList<ImageView>();
    list.add((ImageView) view.findViewById(R.id.image_pin_code1));
    list.add((ImageView) view.findViewById(R.id.image_pin_code2));
    list.add((ImageView) view.findViewById(R.id.image_pin_code3));
    list.add((ImageView) view.findViewById(R.id.image_pin_code4));
    return list;
  }


  private void shuffleKeyboard() {

    Collections.shuffle(numberList);
    int size = buttonList.size();

    for (int i = 0; i < size; ++i) {
      buttonList.get(i).setText(String.valueOf(numberList.get(i)));
    }
  }

  private void doSomeAfterAllPinCode() {
    String pin = password;
    password = "";

//    pinDriver.onPinResult(pin)
    presenter.onEvent(new Event.Builder(Event.RESULT).string(pin).build());
  }

  private void doSomeAfterClickDeleteButton() {
    if (password.length() == 0) {
      return;
    }

    password = password.substring(0, password.length() - 1);
    updateImageAfterPinInput();

  }

  private void doSomeAfterClickNumberButton(View view) {

    int passwordSize = password.length();

    if (passwordSize < 4) {
      password += ((Button) view).getText().toString().charAt(0);

      updateImageAfterPinInput();

      passwordSize = password.length();
      if (passwordSize == 4) {
        Log.e("APP#  PinDialog | onClick", "|" + "if(password.length == 4)");
        doSomeAfterAllPinCode();
      }
    }
  }

  private void updateImageAfterPinInput() {
    int passwordSize = password.length();
    for (int i = 0; i < imageList.size(); ++i) {
      ImageView imageView = imageList.get(i);
      if (i < passwordSize) {
        imageView.setImageResource(R.drawable.circle_full);
      }
      else {
        imageView.setImageResource(R.drawable.circle_empty);
      }
    }
  }

  /**
   * 가이드 텍스트를 업데이트 한다.
   *
   * @param stringId
   */
  public void updateGuideText(@StringRes int stringId) {
    guideString = getString(stringId);

    if (null != textGuide) {
      textGuide.setText(guideString);
    }
  }

  @Override
  public void updateGuideTextColor(int color) {
    textGuide.setTextColor(color);
  }

  @Override
  public void setResetButtonVisible(boolean isVisible) {
    textReset.setVisibility(isVisible ? View.VISIBLE : View.GONE);
  }

  @Override
  public void updateGuideText(String str) {

    if (null != textGuide) {
      textGuide.setText(str);
    }
  }

  /**
   * title 텍스트를 업데이트 한다.
   *
   * @param stringId
   */
  public void updateTitleText(@StringRes int stringId) {
    titleStringId = stringId;

    if (null != textTitle) {
      textTitle.setText(titleStringId);
    }
  }

  @Override
  public void setEnabled() {
    setEnabled(true);
  }

  @Override
  public void setDisabled() {
    setEnabled(false);
  }

  @Override
  public void restart() {
    shuffleKeyboard();
    updateImageAfterPinInput();
    setEnabled(true);
  }

  private void setEnabled(boolean enabled) {
    buttonDelete.setEnabled(enabled);
    buttonShuffle.setEnabled(enabled);
    for (Button button : buttonList) {
      button.setEnabled(enabled);
    }
    for (ImageView imageView : imageList) {
      imageView.setEnabled(enabled);
    }
  }
}
