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

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
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.R;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.model.NoticeData;
import kr.co.hanwha.hifive.data.model.NotificationData;
import kr.co.hanwha.hifive.dialog.HFDefaultDialog;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.enums.LandingType;
import kr.co.hanwha.hifive.enums.MainTab;
import kr.co.hanwha.hifive.screen.main.dashboard.MainDashboardFragment;
import kr.co.hanwha.hifive.screen.main.home.MainHomeFragment;
import kr.co.hanwha.hifive.screen.main.project.MainProjectFragment;
import kr.co.hanwha.hifive.screen.main.setting.MainSettingFragment;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;
import kr.co.hanwha.hifive.util.HFNotiUtil;

/**
 * Main View
 * <p/>
 *
 * @see MainPresenter
 */
public class MainActivity extends BaseActivity {
    private final int mContainerViewId = R.id.container_relativeLayout;

    private MainTab mSelectedTab;
    private MainHomeFragment mFragmentHome;
    private MainProjectFragment mFragmentProject;
    private MainDashboardFragment mFragmentDashboard;

    private int mNotificationValue1 = 0;

    @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);

        findViewById(R.id.tabTask_imageButton).setOnClickListener(onClickListener);
        findViewById(R.id.tabProject_imageButton).setOnClickListener(onClickListener);
        findViewById(R.id.tabDashboard_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);
            }
        });

        // Select Tab
        selectTab(getDefaultTab());

        // Notice
        checkNotice();

        // Notification
        checkNotification();
    }

    private MainTab getDefaultTab() {
        MainTab tab = HFInfoUtil.getDefaultScreen(this);
        NotificationData data = HFInfoUtil.getNotificationData(this);
        if (data != null) {
            int type = 0;
            try {
                if (TextUtils.isEmpty(data.getType()) == false)
                    type = Integer.parseInt(data.getType());

                if (type == LandingType.MAIN_UNIT.getCode()) {
                    tab = MainTab.PROJECT;
                    mNotificationValue1 = Integer.parseInt(data.getValue1());
                } else if (type == LandingType.MAIN_DASHBOARD_UNIT.getCode()) {
                    tab = MainTab.DASHBOARD;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return tab;
    }

    private void checkNotice() {

        NoticeData noticeData = HFInfoUtil.getNoticeData(this);
        if (noticeData != null) {
            int no = noticeData.getId();
            String url = noticeData.getUrl();
            if (TextUtils.isEmpty(url) == false && no != 0)
                HFActivityUtil.startNoticeWebViewActivity(this, url, no);
        }
    }

    private void checkNotification() {
        NotificationData data = HFInfoUtil.getNotificationData(this);

        if (data != null) {
            int type = 0;
            try {
                if (TextUtils.isEmpty(data.getType()) == false)
                    type = Integer.parseInt(data.getType());

                if (type != LandingType.MAIN_UNIT.getCode()) {
                    HFNotiUtil.pendingNotification(this, data);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @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() {
                        HFActivityUtil.startRootActivity(MainActivity.this, true);
                        finish();
                    }
                });

    }

    /**************************************************
     * onActivityResult
     **************************************************/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == HFActivityUtil.REQUEST_ADD_TASK && resultCode == RESULT_OK) {
            if (mSelectedTab == MainTab.HOME && mFragmentHome != null) {
                mFragmentHome.refreshData();
            } else {
                HFInfoUtil.setNeedRefreshMainHomeListAssigned(MainActivity.this);
                HFInfoUtil.setNeedRefreshMainHomeListRequest(MainActivity.this);
            }
        } else if (requestCode == HFActivityUtil.REQUEST_TASK_DETAIL && resultCode == RESULT_OK) {
            if (mSelectedTab == MainTab.HOME && mFragmentHome != null) {
                mFragmentHome.refreshData();
            } else {
                HFInfoUtil.setNeedRefreshMainHomeListAssigned(MainActivity.this);
                HFInfoUtil.setNeedRefreshMainHomeListRequest(MainActivity.this);
            }
        } else if (requestCode == HFActivityUtil.REQUEST_EDIT_TASK_REREQUEST && resultCode == RESULT_OK) {
            if (mSelectedTab == MainTab.HOME && mFragmentHome != null) {
                mFragmentHome.refreshData();
            } else {
                HFInfoUtil.setNeedRefreshMainHomeListAssigned(MainActivity.this);
                HFInfoUtil.setNeedRefreshMainHomeListRequest(MainActivity.this);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }

    }

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

        findViewById(R.id.tabTask_imageButton).setSelected(tab == MainTab.HOME);
        findViewById(R.id.tabProject_imageButton).setSelected(tab == MainTab.PROJECT);
        findViewById(R.id.tabDashboard_imageButton).setSelected(tab == MainTab.DASHBOARD);
        findViewById(R.id.tabSetting_imageButton).setSelected(tab == MainTab.SETTING);

        if (tab == MainTab.HOME) {
            mSelectedTab = tab;
            if (mFragmentHome == null)
                mFragmentHome = new MainHomeFragment();
            replaceFragment(mContainerViewId, mFragmentHome);
        } else if (tab == MainTab.PROJECT) {
            mSelectedTab = tab;
            if (mFragmentProject == null)
                mFragmentProject = new MainProjectFragment();
            mFragmentProject.setProjectId(mNotificationValue1);
            mNotificationValue1 = 0;
            replaceFragment(mContainerViewId, mFragmentProject);
        } else if (tab == MainTab.DASHBOARD) {
            mSelectedTab = tab;
            if (mFragmentDashboard == null)
                mFragmentDashboard = new MainDashboardFragment();
            replaceFragment(mContainerViewId, mFragmentDashboard);
        } else if (tab == MainTab.SETTING) {
            mSelectedTab = 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.tabProject_imageButton:
                    selectTab(MainTab.PROJECT);
                    break;
                case R.id.tabDashboard_imageButton:
                    selectTab(MainTab.DASHBOARD);
                    break;
                case R.id.tabSetting_imageButton:
                    selectTab(MainTab.SETTING);
                    break;
            }
        }
    };
}
