package kr.co.hanwha.hifive.base;

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

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.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.view.HFToastView;

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

public class BaseFragment extends Fragment {
    private HFLoadingDialog mLoadingDialog;

    @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) {
        HFToastView toastView = new HFToastView(getActivity());
        toastView.showToast(message, HFToastView.LENGTH_SHORT);
    }

    /**
     * 토스트 메세지
     *
     * @param message 메세지
     * @param iconRes 아이콘 이미지
     */
    public void showToast(String message, int iconRes) {
        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.temp_ic_tp01);
    }

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

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

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

    public void showAlert(@NonNull final HanwhaNetworkError hanwhaNetworkError) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String title = getTitle(hanwhaNetworkError);
                String message = getMessage(hanwhaNetworkError);
                new HFSingleDialog(getActivity(),
                        title,
                        message,
                        getString(R.string.alert_confirm)).show();
            }
        });
    }

    public void showAlert(@NonNull final HanwhaNetworkError hanwhaNetworkError, final HFSingleDialog.OnDialogCallback callback) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String title = getTitle(hanwhaNetworkError);
                String message = getMessage(hanwhaNetworkError);
                new HFSingleDialog(getActivity(),
                        title,
                        message,
                        getString(R.string.alert_confirm),
                        callback).show();
            }
        });
    }

    public void showAlert(@NonNull final HanwhaNetworkError hanwhaNetworkError, final HFDefaultDialog.OnDialogCallback callback) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String title = getTitle(hanwhaNetworkError);
                String message = getMessage(hanwhaNetworkError);
                new HFDefaultDialog(getActivity(),
                        title,
                        message,
                        getString(R.string.alert_cancel),
                        getString(R.string.retry),
                        callback).show();
            }
        });
    }

    public void showAlert(@NonNull final String title, @NonNull final String message, final HFSingleDialog.OnDialogCallback callback) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new HFSingleDialog(getActivity(),
                        title,
                        message,
                        getString(R.string.alert_confirm),
                        callback).show();
            }
        });
    }

    public void showAlert(@NonNull final String title, @NonNull final String message, final HFDefaultDialog.OnDialogCallback callback) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new HFDefaultDialog(getActivity(),
                        title,
                        message,
                        getString(R.string.alert_cancel),
                        getString(R.string.alert_confirm),
                        callback).show();
            }
        });
    }

    /**
     * Network Error Title
     *
     * @param hanwhaNetworkError Network Error
     * @return String
     */
    private String getTitle(HanwhaNetworkError hanwhaNetworkError) {
        String title = 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 = getString(R.string.alert_network_title);
        } else if (HanwhaNetworkError.CODE_404_NOT_FOUND_ERROR == hanwhaNetworkError.getStatusCode()) {
            title = getString(R.string.alert_default_title);
        }
        return title;
    }

    /**
     * Network Error Message
     *
     * @param hanwhaNetworkError Network Error
     * @return String
     */
    private String getMessage(HanwhaNetworkError hanwhaNetworkError) {
        String message = 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 = getString(R.string.alert_network_message);
        } else if (HanwhaNetworkError.CODE_404_NOT_FOUND_ERROR == hanwhaNetworkError.getStatusCode()) {
            message = getString(R.string.alert_network_serrver_error_message);
        } else if (TextUtils.isEmpty(hanwhaNetworkError.getMessage()) == false) {
            message = hanwhaNetworkError.getMessage();
        }
        return message;
    }
}
