package kr.co.hanwha.hifive.screen.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import java.util.ArrayList;

import kr.co.hanwha.hifive.BuildConfig;
import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.source.TaskDataRepository;
import kr.co.hanwha.hifive.dialog.HFDefaultDialog;
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.enums.MainTab;
import kr.co.hanwha.hifive.network.HFNetworkUtil;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.screen.TestActivity;
import kr.co.hanwha.hifive.screen.main.home.MainHomeFragment;
import kr.co.hanwha.hifive.screen.main.setting.MainSettingFragment;
import kr.co.hanwha.hifive.screen.main.webview.MainWebviewFragment;
import kr.co.hanwha.hifive.util.HFActivityUtil;

public class MainActivity extends BaseActivity {
    private final int mContainerViewId = R.id.container_relativeLayout;

    private MainTab mSelectedTab;
    private MainHomeFragment mFragmentHome;

    private LinearLayout mLayoutTab;
    private Animation mAnimTabVisible = null;
    private Animation mAnimTabGone = null;
    private boolean isTabAni = false;
    private ArrayList<Boolean> animationStack = new ArrayList<Boolean>();

    @Override
    protected int getContentViewResource() {
        return R.layout.activity_main;
    }

    @Override
    protected BaseToolbarType isToolbarStyle() {
        return BaseToolbarType.NONE;
    }

    @Override
    protected BaseAnimationType isShowActivityAnimation() {
        return BaseAnimationType.FADE;
    }

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

        mLayoutTab = (LinearLayout) findViewById(R.id.tab_linearLayout);

        selectTab(MainTab.HOME);

        findViewById(R.id.tabTask_imageButton).setOnClickListener(onClickListener);
        findViewById(R.id.tabDashboard_imageButton).setOnClickListener(onClickListener);
        findViewById(R.id.tabCluster_imageButton).setOnClickListener(onClickListener);
        findViewById(R.id.tabSetting_imageButton).setOnClickListener(onClickListener);

        findViewById(R.id.plus_imageButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HFActivityUtil.startAddTaskActivity(MainActivity.this);
            }
        });

        initAnimation();
    }

    @Override
    public void onBackPressed() {
        showAlert(getString(R.string.alert_finish_title), getString(R.string.alert_finish_message),
                new HFDefaultDialog.OnDialogCallback() {
                    @Override
                    public void onNegative() {
                    }

                    @Override
                    public void onPositive() {
//                        GWActivityUtil.startRootActivity(MainFirstTaskActivity.this, true);
                        finish();
                    }
                });

    }

    /**************************************************
     * onActivityResult
     **************************************************/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == HFActivityUtil.ADD_TASK && resultCode == RESULT_OK) {
            if (mSelectedTab == MainTab.HOME && mFragmentHome != null) {
                mFragmentHome.refreshData();
            } else {
                // TODO ...
            }
        } else if (requestCode == HFActivityUtil.REQUEST_TASK_DETAIL_MODIFY && resultCode == RESULT_OK) {
            if (mSelectedTab == MainTab.HOME && mFragmentHome != null) {
                mFragmentHome.refreshData();
            } else {
                // TODO ...
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }

    }

    /**************************************************
     * Fragment
     **************************************************/
    private void selectTab(@NonNull MainTab tab) {
        mSelectedTab = tab;

        findViewById(R.id.tabTask_imageButton).setSelected(tab == MainTab.HOME);
        findViewById(R.id.tabDashboard_imageButton).setSelected(tab == MainTab.UNIT);
        findViewById(R.id.tabCluster_imageButton).setSelected(tab == MainTab.TEST);
        findViewById(R.id.tabSetting_imageButton).setSelected(tab == MainTab.SETTING);

        if (tab == MainTab.HOME) {
            if (mFragmentHome == null)
                mFragmentHome = new MainHomeFragment();
//            fragment.setOnMainListener(onMainListener);
            replaceFragment(mContainerViewId, mFragmentHome);
        } else if (tab == MainTab.UNIT) {
            showToast(getString(R.string.alert_comming_soon));
            /*
            HFNetworkUtil networkUtil = new HFNetworkUtil();
            networkUtil.test(MainActivity.this, new HanwhaNetworkCallback() {
                @Override
                public void onSuccess(Object response) {
                    showAlert("SUCCESS", "", new HFSingleDialog.OnDialogCallback() {
                        @Override
                        public void onPositive() {

                        }
                    });
                }

                @Override
                public void onFailure(HanwhaNetworkError error) {
                    showAlert(error);
                }

                @Override
                public void onPreExecute() {
                    showLoadingDialog(true);

                }

                @Override
                public void onPostExecute() {
                    showLoadingDialog(false);
                }
            });
            */
        } else if (tab == MainTab.TEST) {
//            showToast(getString(R.string.alert_comming_soon));
            MainWebviewFragment fragment = new MainWebviewFragment();
            fragment.setUrl(BuildConfig.DEV_WEB_URL);
            replaceFragment(mContainerViewId, fragment);
        } else if (tab == MainTab.SETTING) {
            MainSettingFragment fragment = new MainSettingFragment();
            replaceFragment(mContainerViewId, fragment);
        }
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tabTask_imageButton:
                    selectTab(MainTab.HOME);
                    break;
                case R.id.tabDashboard_imageButton:
                    selectTab(MainTab.UNIT);
                    break;
                case R.id.tabCluster_imageButton:
                    selectTab(MainTab.TEST);
                    break;
                case R.id.tabSetting_imageButton:
                    selectTab(MainTab.SETTING);
                    break;
            }
        }
    };

    private void initAnimation() {
        mAnimTabVisible = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.appear_bottom_tab);
        mAnimTabGone = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.disappear_bottom_tab);

        mAnimTabVisible.setAnimationListener(tabVisibleListener);
        mAnimTabGone.setAnimationListener(tabGoneListener);
    }

    private void showToolbar(boolean show) {
        try {
            animationStack.add(show);
            if (isTabAni)
                return;

            if (show && mLayoutTab.getVisibility() != View.VISIBLE) {
                isTabAni = true;
                mLayoutTab.setVisibility(View.VISIBLE);
                mLayoutTab.setAnimation(mAnimTabVisible);
                mLayoutTab.startAnimation(mAnimTabVisible);

//                mLayoutToolbar.setVisibility(View.VISIBLE);
//                mLayoutToolbar.startAnimation(mAnimToolbarVisible);
            } else if (show == false && mLayoutTab.getVisibility() == View.VISIBLE) {
                isTabAni = true;
                mLayoutTab.setAnimation(mAnimTabGone);
                mLayoutTab.startAnimation(mAnimTabGone);

//                mLayoutToolbar.startAnimation(mAnimToolbarGone);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Animation.AnimationListener tabVisibleListener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mLayoutTab.setVisibility(View.VISIBLE);
            isTabAni = false;

            if (animationStack.size() > 0)
                animationStack.remove(0);
            if (animationStack.size() > 0) {
                while (animationStack.size() > 1) {
                    animationStack.remove(0);
                }
                boolean show = animationStack.get(0);
                animationStack.remove(0);
                showToolbar(show);
            }
        }
    };
    Animation.AnimationListener tabGoneListener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mLayoutTab.setVisibility(View.INVISIBLE);
            isTabAni = false;

            if (animationStack.size() > 0)
                animationStack.remove(0);
            if (animationStack.size() > 0) {
                while (animationStack.size() > 1) {
                    animationStack.remove(0);
                }
                boolean show = animationStack.get(0);
                animationStack.remove(0);
                showToolbar(show);
            }
        }
    };
}
