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

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
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.AccountBean;
import com.wd.workoffice.bean.event.AddSummarytEvent;
import com.wd.workoffice.bean.event.AddTicketEvent;
import com.wd.workoffice.bean.mesBean.JobTypeBean;
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.JobTypeAdapter;
import com.wd.workoffice.util.WorkUtils;

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

import java.util.List;
import java.util.Map;

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 MesTicketDetailActivity extends WorkToolBarActivity {
    @BindView(R.id.btn_receive)
    Button btnReceive;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_client)
    TextView tvClient;
    @BindView(R.id.rl_client)
    RelativeLayout rlClient;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.rl_company)
    RelativeLayout rlCompany;
    @BindView(R.id.tv_manager)
    TextView tvManager;
    @BindView(R.id.rl_manager)
    RelativeLayout rlManager;
    @BindView(R.id.tv_clazz)
    TextView tvClazz;
    @BindView(R.id.rv_clazz)
    RecyclerView rvClazz;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_person)
    TextView tvPerson;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_create_person)
    TextView tvCreatePerson;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_take_on)
    TextView tvTakeOn;
    @BindView(R.id.rl_log)
    RelativeLayout rlLog;
    @BindView(R.id.rl_add_log)
    RelativeLayout rlAddLog;
    private TicketBean.ListBean data;
    private Map<String, Object> param;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        String from = getIntent().getStringExtra("from");//0 服务 1 任务担当
        rvClazz.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        data = JSON.parseObject(getIntent().getStringExtra("data"), TicketBean.ListBean.class);
        if (TextUtils.equals(from, "1")) {
            llBottom.setVisibility(View.VISIBLE);
            if (data.getJobStatus() == 1) {
                btnReceive.setVisibility(View.VISIBLE);
            }
            if (data.getJobStatus() == 1 && TextUtils.isEmpty(data.getAcceptUserId())) {
                btnReceive.setVisibility(View.VISIBLE);
            }
            if (data.getJobStatus() == 2) {
                btnSubmit.setVisibility(View.VISIBLE);
                rlLog.setVisibility(View.GONE);
                rlAddLog.setVisibility(View.VISIBLE);
            }
        } else {
            rlAddLog.setVisibility(View.GONE);
        }
        tvCode.setText(data.getJobNo());
        tvClient.setText(data.getCustomerUserName());
        tvCompany.setText(data.getSalesCompanyName());
        tvManager.setText(data.getSalesManagerName());
        tvClazz.setText(data.getJobTypeName());
        tvDesc.setText(data.getJobDescription());
        if (!TextUtils.isEmpty(data.getPeopleCount()))
            tvPerson.setText(data.getPeopleCount());
        tvStartTime.setText(data.getPlanBeginDate());
        tvEndTime.setText(data.getPlanEndDate());
        tvCreatePerson.setText(data.getCreateUserName());
        tvTime.setText(data.getCreateDateTime());
        tvTakeOn.setText(data.getJobUserName());
    }

    private void getType(String id) {
        Map<String, Object> param = WorkUtils.simpleParam();
        param.put("jobTypeCode", id);
        RtfUtils.getMesRtf().jobTypeChild("1", WorkUtils.convertMapToBody(param)).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;
                }
                List<JobTypeBean> jobTypeList = JSON.parseArray(data.getData().toString(), JobTypeBean.class);
                JobTypeAdapter jobTypeAdapter = new JobTypeAdapter(R.layout.item_job_type, jobTypeList);
                jobTypeAdapter.bindToRecyclerView(rvClazz);
            }
        });
    }

    @Override
    protected void initData() {
        getType(data.getJobTypeId());
        param = WorkUtils.simpleParam();
        param.put("jobStatus", 1);
        param.put("jobNo", data.getJobNo());
        param.put("acceptUserId", data.getJobUserId());
        param.put("acceptUserName", data.getJobUserName());
        param.put("jobUserId", data.getJobUserId());
    }


    @Override
    protected void initEvent() {
    }

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


    @OnClick({R.id.btn_receive, R.id.btn_submit, R.id.rl_log, R.id.rl_add_log})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_receive:
                receive();
                break;
            case R.id.btn_submit:
                startActivity(MesTicketAddSummaryActivity.class, "data", JSON.toJSONString(data));
                break;
            case R.id.rl_log:
                startActivity(MesTicketLogActivity.class, "id", data.getJobNo(), "data", JSON.toJSONString(data));
                break;
            case R.id.rl_add_log:
                startActivity(MesTicketLogActivity.class, "id", data.getJobNo(), "data", JSON.toJSONString(data), "from", "1");
//                startActivity(MesTicketAddLogActivity.class, "data", JSON.toJSONString(data));
                break;
        }
    }

    private void receive() {
        RtfUtils.getMesRtf().receiveTicket("1", WorkUtils.convertMapToBody(param)).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;
                }
                toast("接收成功");
                EventBus.getDefault().post(new AddTicketEvent());
                btnSubmit.setVisibility(View.VISIBLE);
                btnReceive.setVisibility(View.GONE);
                rlLog.setVisibility(View.GONE);
                rlAddLog.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

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

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