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 android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatSpinner;
import androidx.appcompat.widget.Toolbar;

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.buildManage.BuildTool;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildReportBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildReportEvent;
import com.tofly.ghrq_route.module.trouble.domain.TroubleAudioBean;
import com.tofly.ghrq_route.module.trouble.domain.TroublePictureBean;
import com.tofly.ghrq_route.common.util.EventBusUtil;

import org.greenrobot.eventbus.Subscribe;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

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

/**
 * Created by Amu on 2017/5/26.
 */

public class UiBuildReportEnrollFirst extends BaseUi {
    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.report_build_name)
    TextView reportBuildName;
    @BindView(R.id.report_build_addr)
    TextView reportBuildAddr;
    @BindView(R.id.report_build_company)
    TextView reportBuildCompany;
    @BindView(R.id.text_build_has_notice)
    TextView  reportBuildHasNotice;
    @BindView(R.id.spinner_build_state)
    AppCompatSpinner spinnerBuildState;
    @BindView(R.id.radio_group_safe)
    RadioGroup radioGroupSafe;
    @BindView(R.id.radio_group_leak)
    RadioGroup radioGroupLeak;
    @BindView(R.id.radio_group_distance)
    RadioGroup radioGroupDistance;
    @BindView(R.id.radio_group_warrant)
    RadioGroup radioGroupWarrant;
    @BindView(R.id.radio_group_pipe)
    RadioGroup radioGroupPipe;
    @BindView(R.id.edit_report_note)
    EditText editReportNote;
    @BindArray(R.array.handle_project_state)
    String[] buildStates;

    private MenuItem menuNext;

    private BuildTool buildTool;

    private BuildManageBean mBuildManageBean = null;

    private BuildReportBean mReportUpload = null;

    private boolean mbHasNotice = false;

    private int mReportState = 0;

    @Subscribe
    public void onMessageEvent(BuildReportEvent event){
        if (event.message.equals(BuildReportEvent.BUILD_REPORT_UPLOADED)){
            this.finish();
        }
    }


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

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

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

        EventBusUtil.register(this);
    }

    private void initView() {
    }

    private void initData() {
        buildTool = BuildTool.getInstance();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            BuildManageBean buildManageBean = (BuildManageBean) bundle.getSerializable("build_manage");
            if (buildManageBean != null) {
                mBuildManageBean = buildManageBean;

                String buildname = buildManageBean.getSiteName();
                reportBuildName.setText(buildname);
                String buildaddr = buildManageBean.getConstructionAddress();
                reportBuildAddr.setText(buildaddr);
                String buildCompany = buildManageBean.getConstructionUnit();
                reportBuildCompany.setText(buildCompany);

     //           mBuildManageBean.setHasBuildNotice(mbHasNotice);

                getUploadNoticeFiles();
            }
        }
    }

    private void initListener() {

    }

    private void getUploadNoticeFiles() {
        if (mBuildManageBean != null) {
            String strUploading = getString(R.string.updating_data);
            ProgressManager.getInstance().showProgress(this, null, strUploading, false);
            buildTool.getBuildNoticeFiles(this, mBuildManageBean, new BuildTool.GetBuildNotice() {
                @Override
                public void buildHasNoticeFile(boolean isSuccess, boolean hasNoticeFile, List<TroublePictureBean> photos, List<TroubleAudioBean> audios) {
                    ProgressManager.getInstance().cancelProgress();
                    if (isSuccess) {
                        mbHasNotice = hasNoticeFile;
                        reportBuildHasNotice.setText(hasNoticeFile ? R.string.yes : R.string.no);

               //         mBuildManageBean.setHasBuildNotice(mbHasNotice);
                        List<String> selectStates = Arrays.asList(buildStates);
                        if (mbHasNotice){
                            selectStates = selectStates.subList(1, buildStates.length);
                        }else {
                            selectStates = selectStates.subList(1, buildStates.length - 1);
                        }
                        ArrayAdapter<String> adapterStates = new ArrayAdapter<>(UiBuildReportEnrollFirst.this, R.layout.my_simple_spinner_item, selectStates);
                        adapterStates.setDropDownViewResource(R.layout.danger_report_spinner_item);
                        spinnerBuildState.setAdapter(adapterStates);
                        if (menuNext != null)
                            menuNext.setEnabled(true);
                    } else {
                        if (menuNext != null)
                            menuNext.setEnabled(false);
                    }
                }
            });
        }
    }

    @OnItemSelected({R.id.spinner_build_state})
    public void onItemSelected(AdapterView<?> parent, int position) {
        switch (parent.getId()) {
            case R.id.spinner_build_state: {
                mReportState = position;
            }
            break;
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_next: {
                setUploadReportData();
                Bundle bundle = new Bundle();
                bundle.putSerializable("report_upload", mReportUpload);
                bundle.putSerializable("build_manage", mBuildManageBean);
                Intent intent = new Intent();
                intent.setClass(this, UiBuildReportEnroll.class);
                intent.putExtras(bundle);
                startActivity(intent);
                overridePendingTransition(R.anim.anim_rightin, R.anim.anim_leftout);
            }
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setUploadReportData(){
        if (mReportUpload == null)
            mReportUpload = new BuildReportBean();
        String report_id = UUID.randomUUID().toString();
        mReportUpload.setId(report_id);
        mReportUpload.setBuild_id(mBuildManageBean.getId());

        String reportNote = editReportNote.getText().toString();
        mReportUpload.setNotes(reportNote);

        mReportUpload.setReport_state(mReportState);

        int isSafe =
                (radioGroupSafe.getCheckedRadioButtonId() == R.id.check_safe_yes) ? 1 : 0;
        mReportUpload.setIs_safemark(isSafe);
        int isDistance =
                (radioGroupDistance.getCheckedRadioButtonId() == R.id.check_distance_yes) ? 1 : 0;
        mReportUpload.setIs_safedistance(isDistance);
        int isWarrant =
                (radioGroupWarrant.getCheckedRadioButtonId() == R.id.check_warrant_yes) ? 1 : 0;
        mReportUpload.setIs_notification(isWarrant);
        int isPipe =
                (radioGroupPipe.getCheckedRadioButtonId() == R.id.check_pipe_yes) ? 1 : 0;
        mReportUpload.setIs_pipeline(isPipe);
        int isLeak =
                (radioGroupLeak.getCheckedRadioButtonId() == R.id.check_leak_yes) ? 1 : 0;
        mReportUpload.setIs_leak(isLeak);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregister(this);
    }
}
