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

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 android.widget.Switch;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.base.BaseFragment;
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.HFInfoUtil;
import kr.co.hanwha.hifive.view.HFSettingMenuActionView;
import kr.co.hanwha.hifive.view.HFSettingMenuUnitView;
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 {
    private HFSettingProfileView mSettingProfileView;
    private HFSettingMenuActionView mMenuViewEditProfile;
    private HFSettingMenuActionView mMenuViewPassword;
    private HFSettingMenuActionView mMenuViewSimplePassword;
    private LinearLayout mLayoutUnit;
    private LinearLayout mLayoutUnitContainer;
    private HFSettingMenuActionView mMenuViewTab;
    private HFSettingMenuActionView mMenuViewHelpdesk;
    private Switch mSwitchAlarm;
    private TextView mTextViewVersion;
    private TextView mTextViewUpdate;

    private MainSettingPresenter mPresenter;

    @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);
        mMenuViewHelpdesk = (HFSettingMenuActionView) parent.findViewById(R.id.helpdesk_settingMenuActionView);
        mSwitchAlarm = (Switch) parent.findViewById(R.id.alarm_switch);
        mTextViewVersion = (TextView) parent.findViewById(R.id.version_textView);
        mTextViewUpdate = (TextView) parent.findViewById(R.id.update_textView);
    }

    @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);
        mMenuViewTab.setOnClickListener(mPresenter.onClickListener);
        mTextViewUpdate.setOnClickListener(mPresenter.onClickListener);
        mTextViewVersion.setOnClickListener(mPresenter.onClickListener);
        mMenuViewHelpdesk.setOnClickListener(mPresenter.onClickListener);

        mSwitchAlarm.setOnCheckedChangeListener(mPresenter.onCheckedChangeListener);
    }

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

        setDefaultScreen();
    }

    @Override
    public void setMemberInfo(@NonNull LoginData loginData) {
        mSettingProfileView.setMemberInfo(loginData);
    }

    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) {
        if (mSwitchAlarm.isChecked() != isChecked)
            mSwitchAlarm.setChecked(isChecked);
    }

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

        mTextViewUpdate.setVisibility(needUpdate ? View.VISIBLE : View.GONE);
    }
}
