package com.phlx.anchorcollect_p.ui.setting;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.phlx.anchorcollect_p.BR;
import com.phlx.anchorcollect_p.R;
import com.phlx.anchorcollect_p.data.VMFactory;
import com.phlx.anchorcollect_p.databinding.ActivitySyncBinding;
import com.phlx.anchorcollect_p.entity.InspectEntity;
import com.phlx.anchorcollect_p.ui.base.BaseActivity;
import com.phlx.anchorcollect_p.ui.labelchange.list.ExchangeListActivity;

import java.util.ArrayList;

public class SyncActivity extends BaseActivity<ActivitySyncBinding,SyncVM> {
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_sync;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
    }

    @Override
    public SyncVM initViewModel() {
        VMFactory factory = VMFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(SyncVM.class);
    }

    @Override
    public void initData() {
        super.initData();
    }

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

        viewModel.errorEvent.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                showTip(s);
            }
        });
        viewModel.inspectEvent.observe(this, new Observer<ArrayList<InspectEntity>>() {
            @Override
            public void onChanged(ArrayList<InspectEntity> inspectEntities) {
                new MaterialDialog.Builder(SyncActivity.this).title("系统信息").content("牛只耳标重复，请修改")
                        .cancelable(false).canceledOnTouchOutside(false)
                        .negativeText("取消")
                        .onNegative(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
                                materialDialog.dismiss();
                                viewModel.dismissDialog();
//                                finish();
                            }
                        })
                        .positiveText("修改").onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                if (which == DialogAction.POSITIVE) {

                                    Bundle mBundle = new Bundle();
                                    mBundle.putParcelableArrayList("inspectEntityList", inspectEntities);
                                    startActivity(ExchangeListActivity.class, mBundle);
                                    dialog.dismiss();
                                    viewModel.dismissDialog();
                                }
                            }
                        }).show();

            }
        });
    }

    private void showTip(String content) {
        new MaterialDialog.Builder(this).title("系统信息").content(content)
                .cancelable(false).canceledOnTouchOutside(false)
                .positiveText("退出").onPositive(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                if (which == DialogAction.POSITIVE) {
                    dialog.dismiss();
                    finish();
                }
            }
        }).show();
    }
}
