package com.wd.workoffice.ui.activity.mes.quality;

import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.event.CheckFeedbackEvent;
import com.wd.workoffice.bean.mesBean.FeedbackDetailBean;
import com.wd.workoffice.bean.mesBean.ImageBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.ImgAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MesQualityDetailActivity extends WorkToolBarActivity {

    @BindView(R.id.btn_reviews)
    Button btnReviews;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.tv_no)
    TextView tvNo;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_manager)
    TextView tvManager;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_pro_name)
    TextView tvProName;
    @BindView(R.id.tv_spec)
    TextView tvSpec;
    @BindView(R.id.tv_img_code)
    TextView tvImgCode;
    @BindView(R.id.tv_box)
    TextView tvBox;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_mark_code)
    TextView tvMarkCode;
    @BindView(R.id.tv_machine)
    TextView tvMachine;
    @BindView(R.id.tv_steel_type)
    TextView tvSteelType;
    @BindView(R.id.tv_defect)
    TextView tvDefect;
    @BindView(R.id.tv_defect_reason)
    TextView tvDefectReason;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_use_status)
    TextView tvUseStatus;
    @BindView(R.id.tv_suggest)
    TextView tvSuggest;
    @BindView(R.id.rv_img)
    RecyclerView rvImg;
    @BindView(R.id.ll_manager)
    LinearLayout llManager;
    @BindView(R.id.tv_factory_reason)
    TextView tvFactoryReason;
    @BindView(R.id.tv_factory_suggest)
    TextView tvFactorySuggest;
    @BindView(R.id.tv_factory_status)
    TextView tvFactoryStatus;
    @BindView(R.id.tv_process)
    TextView tvProcess;
    @BindView(R.id.tv_duty_person)
    TextView tvDutyPerson;
    @BindView(R.id.tv_verify_person)
    TextView tvVerifyPerson;
    @BindView(R.id.tv_question)
    TextView tvQuestion;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.ll_company)
    LinearLayout llCompany;
    @BindView(R.id.tv_client_status)
    TextView tvClientStatus;
    @BindView(R.id.tv_client_reply)
    TextView tvClientReply;
    @BindView(R.id.tv_client_record)
    TextView tvClientRecord;
    @BindView(R.id.tv_client_person)
    TextView tvClientPerson;
    @BindView(R.id.ll_client)
    LinearLayout llClient;
    private FeedbackDetailBean detailBean;
    private List<ImageBean> imgList;
    private ImgAdapter imgAdapter;
    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvImg.setLayoutManager(new GridLayoutManager(this, 3, RecyclerView.VERTICAL, false));

    }


    @Override
    protected void initData() {
        imgList = new ArrayList<>();
        imgAdapter = new ImgAdapter(R.layout.item_mes_ticket_img, imgList);
        imgAdapter.bindToRecyclerView(rvImg);
        detail();
    }

    private void detail() {
        RtfUtils.getMesRtf().feedbackDetail(getIntent().getStringExtra("id"), "1").compose(Transformer.schedule()).subscribe(new WorkObserver<BaseMesBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseMesBean data) {
                hideLoading();
                if (!data.isSuccess()) {
                    toast(data.getMessage());
                    return;
                }
                detailBean = JSON.parseObject(data.getData().toString(), FeedbackDetailBean.class);
                changeView();
            }
        });
    }

    private void changeView() {
        imgList.clear();
        imgList.addAll(detailBean.getAttachments());
        imgAdapter.notifyDataSetChanged();
        switch (detailBean.getStatus()) {
            case "0":
                llClient.setVisibility(View.GONE);
                llCompany.setVisibility(View.GONE);
                llManager.setVisibility(View.GONE);
                llBottom.setVisibility(View.VISIBLE);
                break;
            case "1":
                llClient.setVisibility(View.GONE);
                llCompany.setVisibility(View.GONE);
                llManager.setVisibility(View.GONE);
                llBottom.setVisibility(View.VISIBLE);
                break;
            case "2":
                llClient.setVisibility(View.GONE);
                llCompany.setVisibility(View.GONE);
                llManager.setVisibility(View.VISIBLE);
                llBottom.setVisibility(View.VISIBLE);
                break;
            case "3":
                llClient.setVisibility(View.GONE);
                llCompany.setVisibility(View.VISIBLE);
                llManager.setVisibility(View.VISIBLE);
                llBottom.setVisibility(View.VISIBLE);
                break;
            case "4":
                llClient.setVisibility(View.VISIBLE);
                llCompany.setVisibility(View.VISIBLE);
                llManager.setVisibility(View.VISIBLE);
                llBottom.setVisibility(View.GONE);
                break;
        }
        tvNo.setText(detailBean.getFeedbackCode());
        tvName.setText(detailBean.getCustomerName());
        tvManager.setText(detailBean.getSaleUserName());
        tvPhone.setText(detailBean.getPhoneNumber());
        tvCode.setText(detailBean.getProductCode());
        tvProName.setText(detailBean.getProductName());
        tvSpec.setText(detailBean.getSpecification());
        tvImgCode.setText(detailBean.getDrawingNumber());
        tvBox.setText(detailBean.getBoxCode());
        tvNum.setText(detailBean.getDefectCount());
        tvMarkCode.setText(detailBean.getSerialNumber());
        tvMachine.setText(detailBean.getCasterName());
        tvSteelType.setText(detailBean.getSteelTypeId());
        tvDefect.setText(detailBean.getDefectName());
        tvDefectReason.setText(detailBean.getMainCause());
        //二、销售经理反馈信息
        tvDesc.setText(detailBean.getFeedbackDescription());
        tvUseStatus.setText(detailBean.getOtherProviderBehavior());
        tvSuggest.setText(detailBean.getSalerOpinion());
        //三、公司本部反馈信息
        tvFactoryReason.setText(detailBean.getFactoryAnalysis());
        tvFactoryStatus.setText(detailBean.getFactoryImplement());
        tvFactorySuggest.setText(detailBean.getFactoryLeaderOpinion());
        tvProcess.setText(detailBean.getTreatRectPlan());
        tvDutyPerson.setText(detailBean.getResponsibleUserName());
        tvVerifyPerson.setText(detailBean.getVerifyUserName());
        tvQuestion.setText(detailBean.getDefectMudName());
        tvTime.setText(detailBean.getFactoryTreatmentDateTime());
        //四、客户回馈信息
        tvClientStatus.setText(detailBean.getRapidResponse());
        tvClientReply.setText(detailBean.getCustomerResponse());
        tvClientRecord.setText(detailBean.getRelatedRecord());
        tvClientPerson.setText(detailBean.getRecorUserName());
    }

    @Override
    protected void initEvent() {
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_mes_quality_detail;
    }


    @OnClick(R.id.btn_reviews)
    public void onViewClicked() {
        switch (detailBean.getStatus()) {
            case "0":
                startActivity(MesQualityManagerActivity.class,"data",JSON.toJSONString(detailBean));
                break;
            case "1":
                startActivity(MesQualityManagerActivity.class,"data",JSON.toJSONString(detailBean));
                break;
            case "2":
                startActivity(MesQualityFactoryActivity.class,"data",JSON.toJSONString(detailBean));
                break;
            case "3":
                startActivity(MesQualityClientActivity.class,"data",JSON.toJSONString(detailBean));
                break;
        }
    }
    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void refresh(CheckFeedbackEvent event) {
      finish();
    }
}
