package com.tofly.ghrq_route.common.ui.activity;

import android.annotation.SuppressLint;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.esri.arcgisruntime.mapping.view.MapView;
import com.mgq.base.utils.progressdialog.ProgressManager;
import com.mgq.base.utils.string.StringUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.ui.BaseMapFileActivity;
import com.tofly.ghrq_route.common.ui.view.MyAudioRecordButton;
import com.tofly.ghrq_route.common.util.SnackBarUtil;
import com.tofly.ghrq_route.common.util.dialog.DialogUtil;
import com.tofly.ghrq_route.module.message.controller.MessageCallBack;
import com.tofly.ghrq_route.module.message.controller.MessageController;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.latte_core.base.IPresenter;

import butterknife.BindView;

/**
 * Created by Amu on 2017/4/11.
 */

public class UiReportForHelp extends BaseMapFileActivity {
    @BindView(R.id.mapview)
    MapView mapView;
    @BindView(R.id.my_location)
    ImageView imvMyLocation;
    @BindView(R.id.text_photo_addr)
    TextView textMyAddress;
    @BindView(R.id.dangerreport_et)
    EditText editDangerDetail;
    @BindView(R.id.layout_main_root)
    CoordinatorLayout layoutMainRoot;
    @BindView(R.id.dangerreport1_addpic)
    Button btnAddPicture;
    @BindView(R.id.dangerreport1_gallery)
    LinearLayout layoutGallery;
    @BindView(R.id.layout_addpic_area)
    LinearLayout layoutAddPicArea;
    @BindView(R.id.dangerreport1_addaudio)
    MyAudioRecordButton btnAddAudio;
    @BindView(R.id.audio_record_gallery)
    LinearLayout layoutAudioGallery;
    @BindView(R.id.layout_addaudio_area)
    LinearLayout layoutAddAudioArea;

    private TroubleBean mTroubleUpload = new TroubleBean();

    private MessageController messageController;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_reported_detail, menu);
        menu.findItem(R.id.action_clear).setVisible(false);
        menu.findItem(R.id.action_commit).setTitle(R.string.reporthelp_menu_help);
        return super.onCreateOptionsMenu(menu);
    }

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

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

        textMyAddress.setText("正在定位...");
    }

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

        initListener();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initListener() {

    }

    @Override
    public void onBackPressed() {
        removeCacheFile();
        finish();
    }

    private void collectData() {
        if (mLastLocation != null) {
            mTroubleUpload.setLocationX(StringUtil.formatDigit6(mLastLocation.getX()));
            mTroubleUpload.setLocationY(StringUtil.formatDigit6(mLastLocation.getY()));
        }

        if (mAddress != null) {
            mTroubleUpload.setAddress(mAddress);
        }

        String textInput = editDangerDetail.getText().toString();
        if(!textInput.isEmpty())
            mTroubleUpload.setNotes(textInput);
    }

    private boolean isValid() {
        collectData();

        boolean isTroubleValid = false;
        if (mTroubleUpload != null) {
            if (mTroubleUpload.getLocationX() != null && mTroubleUpload.getLocationY() != null
                    && mTroubleUpload.getAddress() != null) {
                isTroubleValid = true;
            }
        }

        if (!isTroubleValid) {
            showToast("当前位置不可用");
            return false;
        }

        return true;
    }

    private void uploadPhotoReport() {
        String title = "发送求救";
        String content = "是否上传发送求救信息?";
        DialogUtil.showAlertWithConfirmButton(this, title, content, true,
                (dialog, which) -> uploadMessageInfo());
    }

    private void uploadMessageInfo() {
        String strUploading = getString(R.string.reply_sending_info);
        ProgressManager.getInstance().showProgress(this, null, strUploading, false);
        if (messageController == null)
            messageController = new MessageController(this);
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        messageController.sendHelpMsg(userBean.getToken(), mTroubleUpload, mPicturesUpload, mAudiosRecords, new MessageCallBack() {
            @Override
            public void onSendMsg(boolean isSuccess, int errorCode, String errMsg) {
                super.onSendMsg(isSuccess, errorCode, errMsg);
                ProgressManager.getInstance().cancelProgress();
                if (isSuccess) {
                    showUploadFinishedDialog();
                } else {
                    String strWarn = getString(R.string.message_send_info_err) + errMsg;
                    SnackBarUtil.showSnack(layoutMainRoot, strWarn);
                }
            }
        });
    }

    private void showUploadFinishedDialog() {
        DialogUtil.showAlertWithConfirmButton(this, null,
                getString(R.string.message_send_ok), false, (dialog, which) -> {
            removeCacheFile();
            this.finish();
        });
    }

    @Override
    protected IPresenter createPresenter() {
        return null;
    }

    @Override
    public int setLayout() {
        return R.layout.ui_report_help;
    }

    @Override
    public boolean enableLoadMap() {
        return true;
    }

    @Override
    public boolean enableMoveMapListener() {
        return true;
    }

    @Override
    public boolean enableArcgisLocating() {
        return false;
    }

    @Override
    public boolean enableAmapLocationListener() {
        return true;
    }

    @Override
    public boolean enableMapMoveToLocation() {
        return true;
    }

    @Override
    public TextView getTextAddrView() {
        return textMyAddress;
    }

    @Override
    public Button getAddPhotoBtn() {
        return btnAddPicture;
    }

    @Override
    public LinearLayout getPhotoGallery() {
        return layoutGallery;
    }

    @Override
    public MyAudioRecordButton getAudioRecordBtn() {
        return btnAddAudio;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApp.cancelAllRequest(getClass().getName());
        //重置数据状态
    }
}
