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

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.CompoundButton;

import kr.co.hanwha.hifive.BuildConfig;
import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.data.model.CrudDataRoot;
import kr.co.hanwha.hifive.data.model.UpdateData;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.dialog.HFDefaultDialog;
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.util.HFActivityUtil;

public class MainSettingPresenter implements MainSettingContract.Presenter {
    private Activity mActivity;
    private Context mContext;
    private MainSettingContract.View mView;
    private boolean mLastChecked = false;

    public MainSettingPresenter(@NonNull Activity activity, @NonNull Context context, MainSettingContract.View view) {
        mActivity = activity;
        mContext = context;
        mView = view;
    }

    @Override
    public void detachView() {
        mActivity = null;
        mContext = null;
        mView = null;
    }

    @Override
    public void initData() {
        mView.setMemberInfo(MemberDataRepository.getInstance().getLoginData());
        mView.setDefaultScreen();
        mView.setVersion();

        MemberDataRepository.getInstance().getNotificationStatus(getContext(), new HanwhaNetworkCallback<CrudDataRoot>() {
            @Override
            public void onSuccess(CrudDataRoot response) {
                if (response != null) {
                    mLastChecked = response.getOnOff();
                    mView.setAlarmSetting(mLastChecked);
                } else {
                    mView.setAlarmSetting(false);
                }
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                mView.setAlarmSetting(false);
                mView.showAlert(error);
            }

            @Override
            public void onPreExecute() {
                mView.showLoadingDialog(true);
            }

            @Override
            public void onPostExecute() {
                mView.showLoadingDialog(false);
            }
        });
    }

    @Override
    public void doPasswordManagement() {
        HFActivityUtil.startMainSettingEditPasswordWebviewActivity(getActivity());
    }

    @Override
    public void doSimplePasswordManagement() {
        HFActivityUtil.startMainSettingDeleteSimplePasswordWebviewActivity(getActivity());
    }

    @Override
    public void doUnitManagement(@NonNull int projectId) {
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        String encryption = MemberDataRepository.getInstance().getEncryptionMemberId();
        String url = networkUtil.getWebUrlSettingEditUnit();
        HFActivityUtil.startIssueEditActivity(getActivity(),
                url,
                encryption,
                projectId);
    }

    @Override
    public void doUpdate() {
        final UpdateData updateData = MemberDataRepository.getUpdateData();
        if (updateData == null) return;
        
        String title = getContext().getString(R.string.app_name);
        String message = getContext().getString(R.string.alert_update_message);
        if (TextUtils.isEmpty(updateData.getUpdateInfo().getUpdate_notice()) == false)
            message = updateData.getUpdateInfo().getUpdate_notice();

        mView.showAlert(title, message, new HFDefaultDialog.OnDialogCallback() {
            @Override
            public void onNegative() {
            }

            @Override
            public void onPositive() {
                String url = BuildConfig.DOWNLOAD_URL;
                if (TextUtils.isEmpty(updateData.getUpdateInfo().getUpdate_url()) == false)
                    url = updateData.getUpdateInfo().getUpdate_url();
                try {
                    HFActivityUtil.startUrl(getContext(), url);
                    HFActivityUtil.startRootActivity(getActivity(), true);
                    getActivity().finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void setAlarmSetting(boolean isSelected) {
        if (mLastChecked == isSelected) return;
        MemberDataRepository.getInstance().setNotificationStatus(getContext(), isSelected, new HanwhaNetworkCallback<CrudDataRoot>() {
            @Override
            public void onSuccess(CrudDataRoot response) {
                if (response != null) {
                    mLastChecked = response.getOnOff();
                    mView.setAlarmSetting(mLastChecked);
                } else {
                    mView.setAlarmSetting(false);
                }
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                mView.setAlarmSetting(false);
                mView.showAlert(error);
            }

            @Override
            public void onPreExecute() {
                mView.showLoadingDialog(true);
            }

            @Override
            public void onPostExecute() {
                mView.showLoadingDialog(false);
            }
        });
    }

    /**************************************************
     * CompoundButton.OnCheckedChangeListener
     **************************************************/
    public CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            setAlarmSetting(isChecked);
        }
    };

    @Override
    public Context getContext() {
        return mContext;
    }

    private Activity getActivity() {
        return mActivity;
    }


}
