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

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
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.mesBean.ImageBean;
import com.wd.workoffice.bean.mesBean.ReviewBean;
import com.wd.workoffice.bean.mesBean.SummaryBean;
import com.wd.workoffice.bean.mesBean.TicketBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.ImgAdapter;
import com.wd.workoffice.ui.adapter.StarAdapter;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.recyclerview.widget.LinearLayoutManager;
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 MesTicketDetailReviewActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.rv_star)
    RecyclerView rvStar;
    @BindView(R.id.rv_img)
    RecyclerView rvData;
    @BindView(R.id.tv_finish)
    TextView tvFinish;
    @BindView(R.id.tv_design_confirm)
    TextView tvDesignConfirm;
    @BindView(R.id.et_design1)
    EditText etDesign1;
    @BindView(R.id.et_design2)
    EditText etDesign2;
    @BindView(R.id.ll_design)
    LinearLayout llDesign;
    @BindView(R.id.tv_try_pro)
    TextView tvTryPro;
    @BindView(R.id.rl_try_pro)
    RelativeLayout rlTryPro;
    @BindView(R.id.et_try1)
    EditText etTry1;
    @BindView(R.id.et_try2)
    EditText etTry2;
    @BindView(R.id.et_try3)
    EditText etTry3;
    @BindView(R.id.ll_try)
    LinearLayout llTry;
    @BindView(R.id.tv_quality_pro)
    TextView tvQualityPro;
    @BindView(R.id.rl_quality_pro)
    RelativeLayout rlQualityPro;
    @BindView(R.id.et_quality1)
    EditText etQuality1;
    @BindView(R.id.et_quality2)
    EditText etQuality2;
    @BindView(R.id.et_quality3)
    EditText etQuality3;
    @BindView(R.id.et_quality4)
    EditText etQuality4;
    @BindView(R.id.ll_quality)
    LinearLayout llQuality;
    @BindView(R.id.tv_tech_pro)
    TextView tvTechPro;
    @BindView(R.id.rl_tech_pro)
    RelativeLayout rlTechPro;
    @BindView(R.id.et_tech1)
    EditText etTech1;
    @BindView(R.id.et_tech2)
    EditText etTech2;
    @BindView(R.id.et_tech3)
    EditText etTech3;
    @BindView(R.id.ll_tech)
    LinearLayout llTech;
    @BindView(R.id.et_tender1)
    EditText etTender1;
    @BindView(R.id.et_tender2)
    EditText etTender2;
    @BindView(R.id.ll_tender)
    LinearLayout llTender;
    @BindView(R.id.tv_detail)
    TextView tvDetail;
    @BindView(R.id.tv_score)
    TextView tvScore;
    private StarAdapter starAdapter;
    private List<ImageBean> imgList;
    private ImgAdapter imgAdapter;
    private SummaryBean dataBean;
    private TicketBean.ListBean data;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        data = JSON.parseObject(getIntent().getStringExtra("data"), TicketBean.ListBean.class);
        tvCode.setText(data.getJobNo());
        changView();
        rvData.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        rvStar.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
    }

    @Override
    protected void initData() {
        imgList = new ArrayList<>();
        imgAdapter = new ImgAdapter(R.layout.item_mes_ticket_img, imgList);
        imgAdapter.bindToRecyclerView(rvData);
        starAdapter = WorkUtils.starView(rvStar, this);
//        imgList.add(null);
//        imgList.add(null);
//        imgList.add(null);
        imgAdapter.notifyDataSetChanged();
        getData();
    }

    @Override
    protected void initEvent() {
    }

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

    private void getData() {
        RtfUtils.getMesRtf().summaryDetail(data.getJobNo(), "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.getMsg());
                    return;
                }
                dataBean = JSON.parseObject(data.getData().toString(), SummaryBean.class);
                setViewData();
            }
        });
        RtfUtils.getMesRtf().review(data.getJobNo(), "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.getMsg());
                    return;
                }
                ReviewBean dataBean = JSON.parseObject(data.getData().toString(), ReviewBean.class);
                starAdapter.setScore(dataBean.getEvaluationScore());
                tvScore.setText(String.format("%s分", dataBean.getEvaluationScore()));
            }
        });
    }

    private void setViewData() {
        if (TextUtils.equals(data.getJobTypeId(), "01")) {
            tvDesignConfirm.setText(dataBean.getProductDrawingConfirmation() == 0 ? "是" : "否");
            etDesign1.setText(dataBean.getSiteInvestigation());
            etDesign2.setText(dataBean.getFeasibilityReport());
        } else if (TextUtils.equals(data.getJobTypeId(), "02")) {
            etTry1.setText(dataBean.getExperimentProcess());
            etTry2.setText(dataBean.getExperimentResult());
            etTry3.setText(dataBean.getImproveProposal());
        } else if (TextUtils.equals(data.getJobTypeId(), "03")) {
            etQuality1.setText(dataBean.getFeedbackQualityIssues());
            etQuality2.setText(dataBean.getSiteUseSituation());
            etQuality3.setText(dataBean.getCauseAnalysis());
            etQuality4.setText(dataBean.getSolution());
        } else if (TextUtils.equals(data.getJobTypeId(), "04")) {
            etTech1.setText(dataBean.getCommunicativePurposes());
            etTech2.setText(dataBean.getCommunicativeSituation());
            etTech3.setText(dataBean.getReachOpinion());
        } else if (TextUtils.equals(data.getJobTypeId(), "05")) {
            etTender1.setText(dataBean.getProductDevise());
            etTender2.setText(dataBean.getProductQuotedPrice());
        }
        if (!TextUtils.isEmpty(dataBean.getProductsCode())) {
            tvQualityPro.setText(dataBean.getProductsName());
            tvTechPro.setText(dataBean.getProductsName());
            tvTryPro.setText(dataBean.getProductsName());
        }
        tvFinish.setText(dataBean.getCommunicativeSituation());
    }

    private void changView() {
        if (TextUtils.equals(data.getJobTypeId(), "01")) {
            llDesign.setVisibility(View.VISIBLE);
        } else if (TextUtils.equals(data.getJobTypeId(), "02")) {
            llTry.setVisibility(View.VISIBLE);
        } else if (TextUtils.equals(data.getJobTypeId(), "03")) {
            llQuality.setVisibility(View.VISIBLE);
        } else if (TextUtils.equals(data.getJobTypeId(), "04")) {
            llTech.setVisibility(View.VISIBLE);
        } else if (TextUtils.equals(data.getJobTypeId(), "05")) {
            llTender.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.tv_detail})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_detail:
                startActivity(MesTicketDetailActivity.class, "data", JSON.toJSONString(data));
                break;
        }
    }
}
