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

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.base.BaseFragment;
import kr.co.hanwha.hifive.base.FirebaseAnalyticsInterface;
import kr.co.hanwha.hifive.data.model.LoginData;
import kr.co.hanwha.hifive.data.model.ProjectInfoData;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.enums.MainTab;
import kr.co.hanwha.hifive.screen.main.setting.contract.MainSettingContract;
import kr.co.hanwha.hifive.util.HFAnalyticsUtil;
import kr.co.hanwha.hifive.util.HFConstants;
import kr.co.hanwha.hifive.util.HFInfoUtil;
import kr.co.hanwha.hifive.view.HFSettingMenuActionView;
import kr.co.hanwha.hifive.view.HFSettingMenuUnitView;
import kr.co.hanwha.hifive.view.HFSettingMenuVersionView;
import kr.co.hanwha.hifive.view.HFSettingProfileView;

/**
 * MAIN - 관리 탭(4번째)
 *
 * @see MainSettingPresenter
 * Created by sanghoonkwon on 2018. 1. 11..
 */

public class MainSettingFragment extends BaseFragment implements MainSettingContract.View, FirebaseAnalyticsInterface {
    private HFSettingProfileView mSettingProfileView;
    private HFSettingMenuActionView mMenuViewEditProfile;
    private HFSettingMenuActionView mMenuViewPassword;
    private HFSettingMenuActionView mMenuViewSimplePassword;
    private LinearLayout mLayoutUnit;
    private LinearLayout mLayoutUnitContainer;
    private HFSettingMenuActionView mMenuViewTab;
    private HFSettingMenuActionView mMenuViewAlarm;
    private HFSettingMenuVersionView mMenuViewVersion;
    private HFSettingMenuActionView mMenuViewHelpdesk;

    private MainSettingPresenter mPresenter;

    @Override
    public String getClassUri() {
        return HFAnalyticsUtil.HF_GNB_CONFIG;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View parent = inflater.inflate(R.layout.fragment_main_setting, container, false);

        initLayout(parent);

        initPresenter();

        initCallback();

        setCaptainView(MemberDataRepository.getInstance().isCaptain());

        return parent;
    }

    @Override
    public void initLayout(@NonNull View parent) {
        mSettingProfileView = (HFSettingProfileView) parent.findViewById(R.id.settingProfileView);
        mMenuViewEditProfile = (HFSettingMenuActionView) parent.findViewById(R.id.editProfile_settingMenuActionView);
        mMenuViewPassword = (HFSettingMenuActionView) parent.findViewById(R.id.password_settingMenuActionView);
        mMenuViewSimplePassword = (HFSettingMenuActionView) parent.findViewById(R.id.simplePassword_settingMenuActionView);
        mLayoutUnit = (LinearLayout) parent.findViewById(R.id.unit_linearLayout);
        mLayoutUnitContainer = (LinearLayout) parent.findViewById(R.id.unitContainer_linearLayout);
        mMenuViewTab = (HFSettingMenuActionView) parent.findViewById(R.id.tab_settingMenuActionView);
        mMenuViewAlarm = (HFSettingMenuActionView) parent.findViewById(R.id.alarm_settingMenuActionView);
        mMenuViewVersion = (HFSettingMenuVersionView) parent.findViewById(R.id.settingMenuVersionView);
        mMenuViewHelpdesk = (HFSettingMenuActionView) parent.findViewById(R.id.helpdesk_settingMenuActionView);
    }

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

        setDefaultScreen();
        if (HFInfoUtil.getNeedRefreshMainSettingHr(getActivity()))
            mPresenter.initMemberHrInfo(false);

        HFAnalyticsUtil.logEvent(this);
    }

    @Override
    public void initPresenter() {
        mPresenter = new MainSettingPresenter(getActivity(), getActivity(), this);
        mPresenter.initData();
    }

    @Override
    public void initCallback() {
        mSettingProfileView.setOnSettingProfileListener(mPresenter.onSettingProfileListener);

        mMenuViewEditProfile.setOnClickListener(mPresenter.onClickListener);
        mMenuViewPassword.setOnClickListener(mPresenter.onClickListener);
        mMenuViewSimplePassword.setOnClickListener(mPresenter.onClickListener);
        mMenuViewAlarm.setOnSettingMenuActionListener(mPresenter.onSettingMenuActionListener);
        mMenuViewTab.setOnClickListener(mPresenter.onClickListener);
        mMenuViewVersion.setOnSettingMenuVersionListener(mPresenter.onSettingMenuVersionListener);
        mMenuViewHelpdesk.setOnClickListener(mPresenter.onClickListener);
    }

    @Override
    public void setMemberInfo(@NonNull String name, @NonNull String login, @NonNull String rank) {
        mSettingProfileView.setMemberInfo(name, login, rank);
    }

    private void setCaptainView(boolean isCaptain) {
        if (isCaptain) {
            mLayoutUnit.setVisibility(View.VISIBLE);

            mLayoutUnitContainer.removeAllViews();
            ArrayList<ProjectInfoData> list = MemberDataRepository.getInstance().getCaptainProjectInfo();
            for (int i = 0; i < list.size(); i++) {
                final ProjectInfoData data = list.get(i);
                HFSettingMenuUnitView view = new HFSettingMenuUnitView(getActivity());
                view.setProjectInfoData(data, i == 0);
                mLayoutUnitContainer.addView(view);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.doUnitManagement(data.getProject_id());
                    }
                });
            }
        } else {
            mLayoutUnit.setVisibility(View.GONE);
        }
    }

    @Override
    public void setDefaultScreen() {
        MainTab tab = HFInfoUtil.getDefaultScreen(getActivity());
        if (tab == MainTab.HOME)
            mMenuViewTab.setSubName(getString(R.string.main_setting_default_tab_todo));
        else if (tab == MainTab.PROJECT)
            mMenuViewTab.setSubName(getString(R.string.main_setting_default_tab_unit));
        else if (tab == MainTab.DASHBOARD)
            mMenuViewTab.setSubName(getString(R.string.main_setting_default_tab_dashboard));
        else if (tab == MainTab.SETTING)
            mMenuViewTab.setSubName(getString(R.string.main_setting_default_tab_setting));
    }

    @Override
    public void setAlarmSetting(boolean isChecked) {
        mMenuViewAlarm.setAlarmSetting(isChecked);
    }

    @Override
    public void setVersion(@NonNull String version, @NonNull boolean needUpdate) {
        mMenuViewVersion.setVersion(version, needUpdate);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case HFConstants.REQUEST_CHECK_STORAGE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //권한 동의 버튼 선택
                    mPresenter.doUpdate();
                } else {// 사용자가 권한 동의를 안함.
                    mPresenter.showPermissionAlert();
                }
                return;
            }
        }
    }
}
