package kr.co.hanwha.hifive.base;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

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.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.view.HFToastView;

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

public abstract class BaseActivity extends Activity {
    private HFLoadingDialog mLoadingDialog;

    private final int BASE_LAYOUT_RESOURCE_ID = R.layout.activity_base;

    /**
     * 엑티비티 Contents resource
     *
     * @return Resource id
     */
    protected abstract int getContentViewResource();

    /**
     * Toolbar Style<br>
     * Default : NONE<br>
     * NONE<br>
     *
     * @return BaseToolbarType
     */
    protected abstract BaseToolbarType isToolbarStyle();

    /**
     * Activity Animation Type<br>
     * Default : NONE<br>
     * BOTTOM<br>
     * FADE<br>
     * RIGHT<br>
     *
     * @return BaseAnimationType
     */
    protected abstract BaseAnimationType isShowActivityAnimation();

    public Handler baseHandler = new Handler();

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

        checkStartAnimation();

        setContentView(BASE_LAYOUT_RESOURCE_ID);

        int res = getContentViewResource();
        if (res != 0) {
            RelativeLayout layout_baseContainer = (RelativeLayout) findViewById(R.id.baseContainer_relativeLayout);
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(res, layout_baseContainer, true);
        }

        mLoadingDialog = new HFLoadingDialog(this);

        initializeToolbar();
    }

    @Override
    public void finish() {
        super.finish();

        HFActivityUtil.clearTag();

        checkFinishAnimation();

    }

    /**
     * Initialize toolbar
     */
    private void initializeToolbar() {
        if (isToolbarStyle() == BaseToolbarType.DEFAULT) {
            findViewById(R.id.baseTopMargin_view).setVisibility(View.VISIBLE);
            findViewById(R.id.baseToolbar_relativeLayout).setVisibility(View.VISIBLE);
        } else if (isToolbarStyle() == BaseToolbarType.ONLY_BACK) {
            findViewById(R.id.baseTopMargin_view).setVisibility(View.VISIBLE);
            findViewById(R.id.baseBack_imageButton).setVisibility(View.VISIBLE);
            findViewById(R.id.baseToolbar_relativeLayout).setVisibility(View.VISIBLE);
        } else if (isToolbarStyle() == BaseToolbarType.ONLY_CLOSE) {
            findViewById(R.id.baseTopMargin_view).setVisibility(View.VISIBLE);
            findViewById(R.id.baseClose_imageButton).setVisibility(View.VISIBLE);
            findViewById(R.id.baseToolbar_relativeLayout).setVisibility(View.VISIBLE);
        } else if (isToolbarStyle() == BaseToolbarType.ONLY_CLOSE_OVERLAP) {
            findViewById(R.id.baseTopMargin_view).setVisibility(View.GONE);
            findViewById(R.id.baseClose_imageButton).setVisibility(View.VISIBLE);
            findViewById(R.id.baseToolbar_relativeLayout).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.baseTopMargin_view).setVisibility(View.GONE);
            findViewById(R.id.baseToolbar_relativeLayout).setVisibility(View.GONE);
        }

        findViewById(R.id.baseBack_imageButton).setOnClickListener(onClickListener);
        findViewById(R.id.baseClose_imageButton).setOnClickListener(onClickListener);
    }

    /**************************************************
     * ViewActivity.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.baseBack_imageButton:
                    finish();
                    break;
                case R.id.baseClose_imageButton:
                    finish();
                    break;
            }
        }
    };

    /**
     * 엑티비티 시작 애니메이션
     */
    private void checkStartAnimation() {
        if (isShowActivityAnimation() == BaseAnimationType.NONE) {
            overridePendingTransition(0, 0);
        } else if (isShowActivityAnimation() == BaseAnimationType.RIGHT) {
            overridePendingTransition(R.anim.appear_from_right, R.anim.disappear_to_left);
        } else if (isShowActivityAnimation() == BaseAnimationType.BOTTOM) {
            overridePendingTransition(R.anim.appear_from_bottom, 0);
        } else if (isShowActivityAnimation() == BaseAnimationType.FADE) {
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        } else {
            overridePendingTransition(0, 0);
        }
    }

    /**
     * 액티비티 종료 애니메이션
     */
    private void checkFinishAnimation() {
        if (isShowActivityAnimation() == BaseAnimationType.NONE)
            overridePendingTransition(0, 0);
        else if (isShowActivityAnimation() == BaseAnimationType.RIGHT)
            overridePendingTransition(R.anim.appear_from_left, R.anim.disappear_to_right);
        else if (isShowActivityAnimation() == BaseAnimationType.BOTTOM)
            overridePendingTransition(0, R.anim.disappear_to_bottom);
        else if (isShowActivityAnimation() == BaseAnimationType.FADE)
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        else overridePendingTransition(0, 0);
    }

    /**
     * Fragment
     *
     * @param containerViewId Target Layout
     * @param fragment        Fragment
     */
    public void replaceFragment(int containerViewId, Fragment fragment) {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(containerViewId, fragment);
        fragmentTransaction.commit();
    }

    /**
     * 기본 로딩
     *
     * @param show boolean
     */
    public void showLoadingDialog(boolean show) {
        if (show) mLoadingDialog.show();
        else mLoadingDialog.dismiss();
    }

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

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

    /**
     * Network Error Dialog
     *
     * @param hanwhaNetworkError
     */
    public void showAlert(@NonNull final HanwhaNetworkError hanwhaNetworkError) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String title = getTitle(hanwhaNetworkError);
                String message = getMessage(hanwhaNetworkError);
                new HFSingleDialog(BaseActivity.this,
                        title,
                        message,
                        getString(R.string.alert_confirm)).show();
            }
        });
    }

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

    public void showAlert(@NonNull final HanwhaNetworkError hanwhaNetworkError, final HFDefaultDialog.OnDialogCallback callback) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String title = getTitle(hanwhaNetworkError);
                String message = getMessage(hanwhaNetworkError);
                new HFDefaultDialog(BaseActivity.this,
                        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) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new HFSingleDialog(BaseActivity.this,
                        title,
                        message,
                        getString(R.string.alert_confirm),
                        callback).show();
            }
        });
    }

    public void showAlert(@NonNull final String title, @NonNull final String message, final HFDefaultDialog.OnDialogCallback callback) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new HFDefaultDialog(BaseActivity.this,
                        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;
    }
}
