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

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.manager.ActionBarManager;
import kr.co.zumo.app.lifeplus.model.MainModel;
import kr.co.zumo.app.lifeplus.model.SuperModel;
import kr.co.zumo.app.lifeplus.util.AppUtil;
import kr.co.zumo.app.lifeplus.util.StringUtil;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.IMainView;
import kr.co.zumo.app.lifeplus.view.dialog.DialogBase;
import kr.co.zumo.app.lifeplus.view.dialog.DialogBuilder;
import kr.co.zumo.app.lifeplus.view.dialog.DialogID;
import kr.co.zumo.app.lifeplus.view.dialog.IDialogResultListener;
import kr.co.zumo.app.lifeplus.view.presenter.MainPresenter;

/**
 * MainActivity
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 5.]   [최초 작성]
 * @since 2018. 9. 5.
 */
public class MainActivity extends ActivityBase<MainPresenter> implements IMainView {

  @Override
  void onAfterCreate(Bundle savedInstanceState) {

    setContentView(R.layout.activity_main);

    View button;

    button = findViewById(R.id.button_clear);
    button.setOnClickListener(v -> {
      clearData();
    });

    button = findViewById(R.id.button_zumo);
    button.setOnClickListener(v -> {
      SuperModel.getInstance().getPreferences().setZumoUserNo("---------------test--zumo-user----------------");
    });

    button = findViewById(R.id.button_sign_up);
    button.setOnClickListener(v -> {
      presenter.onEvent(new Event.Builder(Event.SIGN_UP).build());
    });

    button = findViewById(R.id.button_faq);
    button.setOnClickListener(v -> {
      presenter.onEvent(new Event.Builder(Event.FAQ).build());
    });

    button = findViewById(R.id.button_pin);
    button.setOnClickListener(v -> {
      presenter.onEvent(new Event.Builder(Event.INPUT_PIN).build());
    });

    button = findViewById(R.id.button_pin_reset);
    button.setOnClickListener(v -> {
      presenter.onEvent(new Event.Builder(Event.RESET).build());
    });

    button = findViewById(R.id.button_setting);
    button.setOnClickListener(v -> {
      Log.w("APP# MainActivity | onAfterCreate", "|" + "button setting click");
      Intent intent = new Intent(MainActivity.this, SettingActivity.class);
      startActivity(intent);
    });

    button = findViewById(R.id.button_delete_account);
    button.setOnClickListener(v -> {
      presenter.onEvent(new Event.Builder(Event.DELETE).build());

      // todo 탈퇴 완료되면 데이터 삭제;
      clearData();
    });

    button = findViewById(R.id.button_set_account);
    button.setOnClickListener(v -> {
      SuperModel.getInstance().setUserId("201810050000000047"); // 효동앱

      render();
    });

    render();

    TextView textView = findViewById(R.id.text_member_status);
    String memberStatus = "";
    if (SuperModel.getInstance().isBlockMember()) {
      memberStatus = "Blocked ";
    }
    if (SuperModel.getInstance().isRestMember()) {
      memberStatus += "Inactive ";
    }
    if (SuperModel.getInstance().isOutMember()) {
      memberStatus += "Seceded";
    }
    if (StringUtil.isFull(memberStatus)) {
      textView.setText("Member status: " + memberStatus);
    }

    ((TextView) findViewById(R.id.text_version)).setText("앱 버젼: " + AppUtil.getVersionName());
  }

  private void clearData() {
    SuperModel.getInstance().getPreferences().setPermissionInfoDone(false);
    SuperModel.getInstance().getPreferences().setTutorialDone(false);
    SuperModel.getInstance().setUserId("");
    SuperModel.getInstance().getPreferences().setEncryptedPin("");
    SuperModel.getInstance().getPreferences().setZumoUserUpdateCompleted(false);
    SuperModel.getInstance().getPreferences().setZumoUserNo("");

    render();
  }

  private void render() {
    findViewById(R.id.button_sign_up).setEnabled(StringUtil.isEmpty(SuperModel.getInstance().getUserId()));
    findViewById(R.id.button_delete_account).setEnabled(StringUtil.isFull(SuperModel.getInstance().getUserId()));
    findViewById(R.id.button_pin).setEnabled(StringUtil.isFull(SuperModel.getInstance().getPreferences().getEncryptedPin()));
    findViewById(R.id.button_pin_reset).setEnabled(StringUtil.isEmpty(SuperModel.getInstance().getPreferences().getEncryptedPin()));
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
  }

  @Override
  void defineActionBar() {
    ActionBarManager actionBarManager = ActionBarManager.getInstance();
    actionBarManager.setProgressVisible(false);
    actionBarManager.setTitleVisible(false);
    actionBarManager.setBiVisible(true);
    actionBarManager.setMenuVisible(true);
    actionBarManager.setSearchVisible(true);
    actionBarManager.setBackVisible(false);
    actionBarManager.show();
  }

  @Override
  MainPresenter definePresenter() {
    return new MainPresenter(getModel(MainModel.class), this);
  }

  private void showAddRegistrationDialog() {
    DialogBase dialog = DialogBuilder.create(DialogID.ADD_REGISTRATION)
      .listener(new IDialogResultListener() {
        @Override
        public void onDialogResult(DialogBase dialog, Event event) {
          switch (event.getEventId()) {
            case Event.EMAIL_CLICK:
              Log.e("APP#  MainFragment | onDialogResult", "|" + "add email button click");
              break;
            case Event.SPECIAL_CODE_CLICK:
              Log.e("APP#  MainFragment | onDialogResult", "|" + "special code button click");
              break;
            default:
              break;
          }
        }

        @Override
        public void onDialogCanceled(DialogBase dialog) {

        }
      })
      .show();
  }

}

