package com.baoshi.swms.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.baoshi.swms.R;
import com.baoshi.swms.net.callback.WasHandlerCallback;
import com.baoshi.swms.net.subscribe.WasSubscribe;
import com.baoshi.swms.util.Hint;

/**
 * 波次恢复 ACTIVITY
 *
 * @author Zhendong Zhou
 * @date   2021/12/3 18:04
 */
public class BatchRecoverActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText editText;
    private String code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_batch_recover);

        loadPartParam();
        registerEvent();
    }

    /**
     * 加载局部变量
     */
    private void loadPartParam(){
        editText = findViewById(R.id.batch_code);
    }

    /**
     * 注册事件
     */
    private void registerEvent() {
        ImageView back = findViewById(R.id.back);
        Button btn = findViewById(R.id.btn);

        editText.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId==EditorInfo.IME_ACTION_DONE){
                batchRecover();
            }
            return true;
        });

        back.setOnClickListener(this);
        btn.setOnClickListener(this);
    }
    private boolean check(){
        code = editText.getText().toString();
        if (code.length()!=13){
            Hint.toastShort(this, "错误的波次号", Hint.COMMON);
            return false;
        }
        return true;
    }
    public void batchRecover(){
        if (!check()){
            return;
        }
        ProgressDialog dialog = Hint.showProgressDialog("修复中", this);
        WasSubscribe.recover(code, new WasHandlerCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
                if (result){
                    Hint.toastShort(BatchRecoverActivity.this, "修复成功", Hint.COMMON);
                }else{
                    Hint.toastShort(BatchRecoverActivity.this, "修复失败", Hint.ERROR);
                }
                super.onSuccess(result);
            }
            @Override
            public void onFinally() {
                dialog.dismiss();
                editText.setText("");
                editText.requestFocus();
            }
        });
    }


    /**
     * 点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.back){
            finish();
        }else{
            batchRecover();
        }
    }
}