package com.philisense.rdgo.ui.user;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.philisense.rdgo.R;
import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.databinding.ActivityPasswordBinding;
import com.philisense.rdgo.http.RequestImpl;
import com.philisense.rdgo.ui.MainActivity;
import com.philisense.rdgo.viewModel.PasswordViewModel;

import io.reactivex.functions.Consumer;

public class PasswordActivity extends AppCompatActivity {

    public ActivityPasswordBinding mBinding;
    public PasswordViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_password);

        Toolbar toolbar = mBinding.include.toolbar;
        toolbar.setTitle("修改密码");
        setSupportActionBar(toolbar);

        toolbar.setNavigationOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                finish();
            }
        });

        viewModel = new PasswordViewModel(
                this,
                RxTextView.textChanges(mBinding.passwordOriginEdit),
                RxTextView.textChanges(mBinding.passwordNewEdit),
                RxTextView.textChanges(mBinding.passwordRenewEdit)
                );

        initViewValidation(mBinding);
    }

    public void initViewValidation(ActivityPasswordBinding binding) {

        viewModel.passwordValidation.subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                if (integer == 1){
                    binding.passwordButton.setEnabled(false);
                    binding.errorText.setText("请填写缺少的选项");
                }else if (integer == 2){
                    binding.passwordButton.setEnabled(false);
                    binding.errorText.setText("两次密码不一致");
                }else if (integer == 3) {
                    binding.passwordButton.setEnabled(false);
                    binding.errorText.setText("新旧密码不能相同");
                } else {
                    binding.passwordButton.setEnabled(true);
                    binding.errorText.setText("");
                }
            }
        });

        RxView.clicks(binding.passwordButton).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                final ProgressDialog dialog = ProgressDialog.show(PasswordActivity.this,"提示","正在修改密码..");
                viewModel.modifyPassword(new RequestImpl() {
                    @Override
                    public void loadSuccess(Object object) {
                        dialog.dismiss();
                        Toast.makeText(PasswordActivity.this,"修改成功",Toast.LENGTH_LONG).show();
                        finish();
                    }

                    @Override
                    public void loadFailed(OError error) {
                        dialog.dismiss();
                        Snackbar.make(mBinding.rootLayout,error.getErrorMsg(),Snackbar.LENGTH_LONG).show();
                    }
                });

            }
        });
    }

    public static void start(Activity context) {
        Intent intent = new Intent(context, PasswordActivity.class);
        ActivityCompat.startActivity(context, intent,null);
    }

}
