package com.tofly.ghrq_route.module.trouble.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatSpinner;

import com.mgq.base.utils.progressdialog.ProgressManager;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.module.trouble.TroubleTool;
import com.tofly.ghrq_route.module.trouble.controller.TroubleCallBack;
import com.tofly.ghrq_route.module.trouble.domain.MyTroubleEvent;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemSelected;

public class UiTroubleSectaryVerify extends BaseUi {
    @BindView(R.id.spinner_trouble_verify_state)
    AppCompatSpinner spinnerVerifyState;
    @BindView(R.id.edit_verify_note)
    EditText editVerifyNote;
    @BindArray(R.array.trouble_sectary_verify_states)
    String[] verifyStates;

    private int mVerifyState = 0;

    private TroubleBean mTroubleBean;

    private TroubleTool troubleTool;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ui_trouble_sectary_verify);
        ButterKnife.bind(this);

        initView();
        initData();
        initListener();
    }

    private void initView() {
        setDarkStatusBar();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            mTroubleBean = (TroubleBean) bundle.getSerializable(TroubleBean.TROUBLE_BEAN_KEY);
        }
    }

    private void initData() {
        troubleTool = TroubleTool.getInstance();

        setVerifyStateAdapter();
        spinnerVerifyState.setSelection(0);
    }

    private void initListener() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_commit, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.action_commit) {
            hideSoftKeyboard();
            if (isValid()) {
                uploadTroubleAlert();
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @OnItemSelected({R.id.spinner_trouble_verify_state})
    public void onItemSelected(AdapterView<?> parent, int position) {
        if (parent.getId() == R.id.spinner_trouble_verify_state) {
            mVerifyState = position;
        }
    }

    private void setVerifyStateAdapter() {
        ArrayAdapter<String> adapterDevType = new ArrayAdapter<String>(this,
                R.layout.my_simple_spinner_item, verifyStates) {
        };
        adapterDevType.setDropDownViewResource(R.layout.danger_report_spinner_item);
        //加载适配器
        spinnerVerifyState.setAdapter(adapterDevType);
    }

    private boolean isValid() {
        if (editVerifyNote.getText().toString().isEmpty()) {
            editVerifyNote.requestFocus();
            toast(R.string.trouble_undersectary_verify_note_hint);
            return false;
        }

        return true;
    }

    private void collectData() {
        /*审核详情*/
        String notes = editVerifyNote.getText().toString();
        mTroubleBean.setNotes(notes);

        mTroubleBean.setState(mVerifyState);
        mTroubleBean.setStateName(verifyStates[mVerifyState]);
    }

    private void uploadTroubleAlert() {
        String title = "上传数据";
        String content = "是否上传数据?";
        showAlertWithConfirmCancelButton(title, content, true, (dialogInterface, i) -> uploadTroubleData());
    }

    private void uploadTroubleData() {
        collectData();
        String strUploading = getString(R.string.uploading_data);
        ProgressManager.getInstance().showProgress(this, null, strUploading, false);
        troubleTool.sectaryVerifySchemeNoSave(this, mTroubleBean, new TroubleCallBack() {
            @Override
            public void onSetData(boolean isSuccess, int errorCode, String errMsg) {
                super.onSetData(isSuccess, errorCode, errMsg);
                ProgressManager.getInstance().cancelProgress();
                if (isSuccess) {
                    EventBus.getDefault().post(new MyTroubleEvent(MyTroubleEvent.TROUBLE_UPLOADED));
                    showUploadFinishedDialog();
                }
            }
        });
    }

    private void showUploadFinishedDialog() {
        int title = R.string.trouble_sectary_verify_title;
        int msg = R.string.upload_ok;
        showAlertWithConfirmButton(title, msg, false, (dialogInterface, i) -> this.finish());
    }
}
