package kr.co.hanwha.hifive.base;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.TextUtils;

import kr.co.hanwha.hifive.HFApplication;
import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.dialog.HFDefaultDialog;
import kr.co.hanwha.hifive.dialog.HFLoadingDialog;
import kr.co.hanwha.hifive.dialog.HFSingleDialog;
import kr.co.hanwha.hifive.enums.AlertType;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.view.HFToastView;

/**
 * Created by sanghoonkwon on 2018. 1. 25..
 */

public class BaseFragment extends Fragment implements FirebaseAnalyticsInterface {
    private HFLoadingDialog mLoadingDialog;

    @Override
    public String getClassUri() {
        return "";
    }

    @Override
    public String getClassName() {
        return getClass().getSimpleName();
    }

    @Override
    public Context getClassContext() {
        return getActivity();
    }

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

        mLoadingDialog = new HFLoadingDialog(getActivity());
    }

    public void showLoadingDialog(boolean show) {
        if (show) mLoadingDialog.show();
        else mLoadingDialog.dismiss();
    }

    /**
     * 토스트 메세지
     *
     * @param message 메세지
     */
    public void showToast(String message) {
        if (getActivity() == null) return;
        HFToastView toastView = new HFToastView(getActivity());
        toastView.showToast(message, HFToastView.LENGTH_SHORT);
    }

    /**
     * 토스트 메세지
     *
     * @param message 메세지
     * @param iconRes 아이콘 이미지
     */
    public void showToast(String message, int iconRes) {
        if (getActivity() == null) return;
        HFToastView toastView = new HFToastView(getActivity());
        toastView.showToast(message, iconRes, HFToastView.LENGTH_SHORT);
    }

    /**
     * @param strResource String resource
     */
    public void showToast(int strResource) {
        showToast(getString(strResource));
    }

    public void showToastAlert(int strResource) {
        showToast(getString(strResource), R.mipmap.ic_tp01);
    }

    public void showToastEmpty(int strResource) {
        showToast(getString(strResource), R.mipmap.ic_tp02);
    }

    public void showToastConfirm(int strResource) {
        showToast(getString(strResource, R.mipmap.ic_tp03));
    }

    public void showToastImpossible(int strResource) {
        showToast(getString(strResource), R.mipmap.ic_tp04);
    }

    public void showToastAlert(String message) {
        showToast(message, R.mipmap.ic_tp01);
    }

    public void showToastEmpty(String message) {
        showToast(message, R.mipmap.ic_tp02);
    }

    public void showToastConfirm(String message) {
        showToast(message, R.mipmap.ic_tp03);
    }

    public void showToastImpossible(String message) {
        showToast(message, R.mipmap.ic_tp04);
    }

    /**
     * Network Error Dialog
     *
     * @param hanwhaNetworkError
     */
    public void showAlert(@NonNull HanwhaNetworkError hanwhaNetworkError) {
        showAlertSingle(getTitle(hanwhaNetworkError), getMessage(hanwhaNetworkError), null, null);
    }

    public void showAlert(@NonNull HanwhaNetworkError hanwhaNetworkError, HFSingleDialog.OnDialogCallback callback) {
        showAlertSingle(getTitle(hanwhaNetworkError), getMessage(hanwhaNetworkError), null, callback);
    }

    public void showAlert(@NonNull HanwhaNetworkError hanwhaNetworkError, HFDefaultDialog.OnDialogCallback callback) {
        showAlertDouble(getTitle(hanwhaNetworkError), getMessage(hanwhaNetworkError), getString(R.string.alert_cancel), getString(R.string.retry), callback);
    }

    public void showAlert(@NonNull String title, @NonNull String message, HFSingleDialog.OnDialogCallback callback) {
        showAlertSingle(title, message, getString(R.string.alert_confirm), callback);
    }

    public void showAlert(@StringRes int title, @StringRes int message, HFSingleDialog.OnDialogCallback callback) {
        showAlertSingle(getActivity().getString(title), getActivity().getString(message), getString(R.string.alert_confirm), callback);
    }

    public void showAlert(@NonNull String title, @NonNull String message, @NonNull String confirm, HFSingleDialog.OnDialogCallback callback) {
        showAlertSingle(title, message, confirm, callback);
    }

    public void showAlert(@NonNull String title, @NonNull String message, HFDefaultDialog.OnDialogCallback callback) {
        showAlertDouble(title, message, getString(R.string.alert_cancel), getString(R.string.alert_confirm), callback);
    }

    public void showAlert(@NonNull String title, @NonNull String message, @Nullable String cancel, @Nullable String confirm, HFDefaultDialog.OnDialogCallback callback) {
        showAlertDouble(title, message, cancel, confirm, callback);
    }

    public void showAlertTypeB(@NonNull String title, @NonNull String message, @Nullable String cancel, @Nullable String confirm, HFDefaultDialog.OnDialogCallback callback) {
        showAlertDouble(title, message, cancel, confirm, AlertType.TYPE_B, callback);
    }

    private void showAlertSingle(@NonNull final String title, @NonNull final String message, @Nullable final String confirm, @Nullable final HFSingleDialog.OnDialogCallback callback) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (getActivity().isFinishing() == false) {
                    new HFSingleDialog(getActivity(),
                            title,
                            message,
                            TextUtils.isEmpty(confirm) ? getString(R.string.alert_confirm) : confirm,
                            callback).show();
                }
            }
        });
    }

    private void showAlertDouble(@NonNull String title,
                                 @NonNull String message,
                                 @Nullable String cancel,
                                 @Nullable String confirm,
                                 @Nullable HFDefaultDialog.OnDialogCallback callback) {
        showAlertDouble(title, message, cancel, confirm, null, callback);
    }

    private void showAlertDouble(@NonNull final String title,
                                 @NonNull final String message,
                                 @Nullable final String cancel,
                                 @Nullable final String confirm,
                                 @Nullable final AlertType alertType,
                                 @Nullable final HFDefaultDialog.OnDialogCallback callback) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (getActivity().isFinishing() == false) {
                    new HFDefaultDialog(getActivity(),
                            title,
                            message,
                            TextUtils.isEmpty(cancel) ? getString(R.string.alert_cancel) : cancel,
                            TextUtils.isEmpty(confirm) ? getString(R.string.alert_confirm) : confirm,
                            alertType == null ? AlertType.DEFAULT : alertType,
                            callback).show();
                }
            }
        });
    }

    /**
     * Network Error Title
     *
     * @param hanwhaNetworkError Network Error
     * @return String
     */
    private String getTitle(HanwhaNetworkError hanwhaNetworkError) {
        String title = HFApplication.getAppContext().getString(R.string.alert_default_title);
        if (hanwhaNetworkError == null) return title;
        if (HanwhaNetworkError.CODE_NETWORK_CONNECTION_ERROR == hanwhaNetworkError.getStatusCode() ||
                HanwhaNetworkError.CODE_CONNECT_TIMED_OUT == hanwhaNetworkError.getStatusCode()) {
            title = HFApplication.getAppContext().getString(R.string.alert_network_title);
        } else if (HanwhaNetworkError.CODE_404_NOT_FOUND_ERROR == hanwhaNetworkError.getStatusCode()) {
            title = HFApplication.getAppContext().getString(R.string.alert_default_title);
        }
        return title;
    }

    /**
     * Network Error Message
     *
     * @param hanwhaNetworkError Network Error
     * @return String
     */
    private String getMessage(HanwhaNetworkError hanwhaNetworkError) {
        String message = HFApplication.getAppContext().getString(R.string.alert_default_message);
        if (hanwhaNetworkError == null) return message;
        if (HanwhaNetworkError.CODE_NETWORK_CONNECTION_ERROR == hanwhaNetworkError.getStatusCode() ||
                HanwhaNetworkError.CODE_CONNECT_TIMED_OUT == hanwhaNetworkError.getStatusCode()) {
            message = HFApplication.getAppContext().getString(R.string.alert_network_message);
        } else if (HanwhaNetworkError.CODE_404_NOT_FOUND_ERROR == hanwhaNetworkError.getStatusCode()) {
            message = HFApplication.getAppContext().getString(R.string.alert_network_serrver_error_message);
        } else if (TextUtils.isEmpty(hanwhaNetworkError.getMessage()) == false) {
            message = hanwhaNetworkError.getMessage();
        }
        return message;
    }
}
