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

import android.content.Intent;
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.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.BuildConfig;
import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.admin.AdminInputDialog;
import kr.co.hanwha.hifive.admin.TestModeActivity;
import kr.co.hanwha.hifive.admin.admin.AdminModeActivity;
import kr.co.hanwha.hifive.admin.design.TestDesignActivity;
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.intro.IntroActivity;
import kr.co.hanwha.hifive.util.GlideUtil;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;
import kr.co.hanwha.hifive.view.HFSettingMenuUnitView;

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

public class MainSettingFragment extends BaseFragment implements MainSettingContract.View {
    private TextView mTextViewName;
    private TextView mTextViewPosition1;
    private View mViewLine1;
    private TextView mTextViewId;

    private LinearLayout mLayoutDepartment;
    private TextView mTextViewPosition2;
    private View mViewLine2;
    private TextView mTextViewDepartment;
    private ImageView mImageViewProfile;

    private LinearLayout mLayoutPassword;
    private LinearLayout mLayoutSimplePassword;
    private LinearLayout mLayoutUnit;
    private LinearLayout mLayoutUnitContainer;
    private LinearLayout mLayoutTab;
    private TextView mTextViewTab;
    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);

        mTextViewName = (TextView) parent.findViewById(R.id.name_textView);
        mTextViewPosition1 = (TextView) parent.findViewById(R.id.position1_textView);
        mViewLine1 = (View) parent.findViewById(R.id.line1_view);
        mTextViewId = (TextView) parent.findViewById(R.id.id_textView);

        mLayoutDepartment = (LinearLayout) parent.findViewById(R.id.department_linearLayout);
        mTextViewPosition2 = (TextView) parent.findViewById(R.id.position2_textView);
        mViewLine2 = (View) parent.findViewById(R.id.line2_view);
        mTextViewDepartment = (TextView) parent.findViewById(R.id.department_textView);
        mImageViewProfile = (ImageView) parent.findViewById(R.id.profile_imageView);

        mLayoutPassword = (LinearLayout) parent.findViewById(R.id.password_linearLayout);
        mLayoutSimplePassword = (LinearLayout) parent.findViewById(R.id.simplePassword_linearLayout);
        mLayoutUnit = (LinearLayout) parent.findViewById(R.id.unit_linearLayout);
        mLayoutUnitContainer = (LinearLayout) parent.findViewById(R.id.unitContainer_linearLayout);
        mLayoutTab = (LinearLayout) parent.findViewById(R.id.tab_linearLayout);
        mTextViewTab = (TextView) parent.findViewById(R.id.tab_textView);
        mSwitchAlarm = (Switch) parent.findViewById(R.id.alarm_switch);
        mTextViewVersion = (TextView) parent.findViewById(R.id.version_textView);
        mTextViewUpdate = (TextView) parent.findViewById(R.id.update_textView);

        GlideUtil.getInstance().loadImageCircle(getActivity(), R.mipmap.ic_avatar, mImageViewProfile);

        mLayoutPassword.setOnClickListener(onClickListener);
        mLayoutSimplePassword.setOnClickListener(onClickListener);
        mLayoutTab.setOnClickListener(onClickListener);
        mTextViewUpdate.setOnClickListener(onClickListener);
        mTextViewVersion.setOnClickListener(onClickListener);

        mPresenter = new MainSettingPresenter(getActivity(), getActivity(), this);
        mPresenter.initData();
        mSwitchAlarm.setOnCheckedChangeListener(mPresenter.onCheckedChangeListener);

        mImageViewProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testIndex += 1;
                testHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        testIndex = 0;

                    }
                }, 5000);
                if (testIndex > 5 && testIndex < 10) {
                    HFInfoUtil.setMemberLoginId(getActivity(), "");
                    HFActivityUtil.startIntroActivity(getActivity());
                }
            }
        });

        return parent;
    }

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

        setDefaultScreen();
    }

    @Override
    public void setMemberInfo(@NonNull LoginData loginData) {
        if (loginData == null) return;

        mTextViewName.setText(loginData.getName());

        mTextViewId.setText(loginData.getLogin());

        String position = loginData.getPosition();

        String department = HFInfoUtil.getDepartmentText(getActivity());
        if (TextUtils.isEmpty(department)) {
            if (TextUtils.isEmpty(position)) {
                mViewLine1.setVisibility(View.GONE);
                mTextViewPosition1.setVisibility(View.GONE);
            } else {
                mTextViewPosition1.setText(position);
                mViewLine1.setVisibility(View.VISIBLE);
                mTextViewPosition1.setVisibility(View.VISIBLE);
            }

            mLayoutDepartment.setVisibility(View.GONE);
        } else {
            mTextViewPosition1.setVisibility(View.GONE);
            mViewLine1.setVisibility(View.GONE);

            if (TextUtils.isEmpty(position)) {
                mViewLine2.setVisibility(View.GONE);
                mTextViewPosition2.setVisibility(View.GONE);
            } else {
                mTextViewPosition2.setText(position);
                mViewLine2.setVisibility(View.VISIBLE);
                mTextViewPosition2.setVisibility(View.VISIBLE);
            }

            mLayoutDepartment.setVisibility(View.VISIBLE);
            mTextViewDepartment.setText(department);
        }

        if (MemberDataRepository.getInstance().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)
            mTextViewTab.setText(getString(R.string.main_setting_default_tab_todo));
        else if (tab == MainTab.PROJECT)
            mTextViewTab.setText(getString(R.string.main_setting_default_tab_unit));
        else if (tab == MainTab.DASHBOARD)
            mTextViewTab.setText(getString(R.string.main_setting_default_tab_dashboard));
        else if (tab == MainTab.SETTING)
            mTextViewTab.setText(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() {
        String lastVersion = MemberDataRepository.getLastVersion();
        String currentVersion = HFCommonUtil.getAppVersion(getActivity());
        String rc = "-" + BuildConfig.RC;
        String revision = " (r" + BuildConfig.REVISION + ")";

        if (lastVersion != null && currentVersion != null && lastVersion.compareTo(currentVersion) <= 0) {
            mTextViewVersion.setText("v" + currentVersion + rc + revision);
            mTextViewUpdate.setVisibility(View.GONE);
        } else {
            mTextViewVersion.setText("v" + currentVersion + rc + revision);
            mTextViewUpdate.setVisibility(View.VISIBLE);
        }
    }


    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.password_linearLayout:
                    mPresenter.doPasswordManagement();
                    break;
                case R.id.simplePassword_linearLayout:
                    mPresenter.doSimplePasswordManagement();
                    break;
                case R.id.tab_linearLayout:
                    HFActivityUtil.startSettingScreenActivity(getActivity());
                    break;
                case R.id.update_textView:
                    mPresenter.doUpdate();
                    break;
                case R.id.version_textView:
                    testIndex += 1;
                    testHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            testIndex = 0;

                        }
                    }, 5000);
                    if (testIndex > 10 && testIndex < 20) {
                        showAdmin();
                    }
                    break;
            }
        }
    };

    /**************************************************
     * ADMIN
     **************************************************/
    Handler testHandler = new Handler();
    int testIndex = 0;

    private void showAdmin() {
        new AdminInputDialog(getActivity(), AdminInputDialog.ADMIN_TYPE.PASSWORD, new AdminInputDialog.OnDialogCallback() {
            @Override
            public void onPositive(@NonNull String message) {
                Intent intent = new Intent(getActivity(), TestModeActivity.class);
                startActivity(intent);
            }

            @Override
            public void onWrongPassword() {
                showToast("Wrong password");
            }
        }).show();
    }
}
