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

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.appcompat.widget.AppCompatSpinner;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.mgq.base.utils.progressdialog.ProgressManager;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.module.systemConfig.controller.ServerSysConfigCallBack;
import com.tofly.ghrq_route.module.systemConfig.domain.ServerSysConfigBean;
import com.tofly.ghrq_route.module.uploadPhoto.UploadPhotoTool;
import com.tofly.ghrq_route.module.uploadPhoto.domain.UploadPhotoBean;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemSelected;

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

public class UiPhotoReport extends BaseUi {
    @BindView(R.id.layout_main_root)
    CoordinatorLayout layoutMainRoot;
    @BindView(R.id.spinner_photo_type)
    AppCompatSpinner spinnerPhotoType;
    @BindView(R.id.edit_photo_note)
    EditText editPhotoDetail;

    private List<ServerSysConfigBean> mPhotoTypeConfigList;
    private List<String> mPhotoTypeNameList;
    public int mPhotoState = 0;

    private UploadPhotoTool uploadPhotoTool;

    private UploadPhotoBean mPhotoUpload = new UploadPhotoBean();

    public static UiPhotoReport uiPhotoReport = null;


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

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

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

        uiPhotoReport = this;
    }

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

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

    protected void initView() {

    }

    protected void initData() {
        uploadPhotoTool = UploadPhotoTool.getInstance();

        getPhotoTypeList();
    }


    protected void initListener() {

    }

    private void getPhotoTypeList() {
        String strUploading = getString(R.string.photoreport_getting_phototype);
        ProgressManager.getInstance().showProgress(this, null, strUploading, false);
        uploadPhotoTool.getPhotoTypeConfigList(this, new ServerSysConfigCallBack(){
            @Override
            public void onGetSysConfigList(boolean isSuccess, int errorCode, String errMsg, List<ServerSysConfigBean> configs, int total) {
                super.onGetSysConfigList(isSuccess, errorCode, errMsg, configs, total);
                ProgressManager.getInstance().cancelProgress();
                if (isSuccess && configs != null) {
                    mPhotoTypeConfigList = configs;
                    mPhotoTypeNameList = new ArrayList<>();
                    for (ServerSysConfigBean configBean : configs) {
                        mPhotoTypeNameList.add(configBean.getValue());
                    }
                    setEquipmentAdater();
                }
            }
        });
    }

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

    private boolean isValid() {
        boolean isPhotoTypeValid = false;

        if (mPhotoTypeConfigList != null && mPhotoTypeConfigList.size() > 0) {
            isPhotoTypeValid = true;
        }

        if (!isPhotoTypeValid) {
            String strWarn = getString(R.string.photoreport_phototype_empty_err);
            toast(strWarn);
            return false;
        }

        String description = editPhotoDetail.getText().toString();
        if (description.isEmpty()) {
            String strWarn = getString(R.string.photoreport_photonote_empty_err);
            toast(strWarn);
            return false;
        }

        return true;
    }


    private void collectData(){
        mPhotoUpload.setId(UUID.randomUUID().toString());
        String description = editPhotoDetail.getText().toString();
        mPhotoUpload.setNotes(description);
        if (mPhotoTypeConfigList != null) {
            ServerSysConfigBean photoTypeConfig = mPhotoTypeConfigList.get(mPhotoState);
            if (photoTypeConfig != null) {
                mPhotoUpload.setPicTypeId(photoTypeConfig.getId());
            }
        }
    }

    private void gotoPhotoReportNext() {
        collectData();

        Bundle bundle = new Bundle();
        bundle.putSerializable(UploadPhotoBean.UPLOAD_PHOTO_BEAN_KEY, mPhotoUpload);

        Intent intent = new Intent();
        intent.setClass(this, UiPhotoReportNext.class);
        intent.putExtras(bundle);
        startActivity(intent);
        overridePendingTransition(R.anim.anim_rightin, R.anim.anim_leftout);
    }

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

    @Override
    protected void onDestroy() {
        super.onDestroy();

        BaseApp.cancelAllRequest(getClass().getName());
    }
}
