package com.bbitcn.silk.ui.activity;

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

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bbitcn.silk.R;
import com.bbitcn.silk.adapter.ConvertAdapter;
import com.bbitcn.silk.adapter.ItemContentAdapter;
import com.bbitcn.silk.base.BaseActivity;
import com.bbitcn.silk.databinding.ActivityConvertBinding;
import com.bbitcn.silk.databinding.ItemConvertBinding;
import com.bbitcn.silk.databinding.ItemInfoContentBinding;
import com.bbitcn.silk.model.InfoContent;
import com.bbitcn.silk.model.net.QueryList;
import com.bbitcn.silk.model.net.ToConvert;
import com.bbitcn.silk.utils.database.AllTypeDataBase;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;

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

/**
 * @Author DuanKaiji
 * @CreateTime 2024年03月27日 15:12
 */
public class ConvertActivity extends BaseActivity<ActivityConvertBinding, ConvertActivityPresenter> {
    QueryList.DataDTO data;

    @Override
    public void initView() {
        data = GsonUtils.fromJson(getIntent().getStringExtra("data"), QueryList.DataDTO.class);
        binding.include.tvName.setText(data.getNhName());
        binding.include.tvId.setText(data.getCzCode());
        binding.include.recycleView.setLayoutManager(new LinearLayoutManager(mContext));
        List<InfoContent> temp = new ArrayList<>();
        for (QueryList.DataDTO.ItemsDTO itemsDTO : data.getItems()) {
            temp.add(new InfoContent(itemsDTO.getSgTypeName(), itemsDTO.getBaoshu(), itemsDTO.getMaozhong(), itemsDTO.getPrice()));
        }
        binding.include.cardState.setVisibility(View.GONE);
        binding.include.recycleView.setAdapter(new ItemContentAdapter(temp, ItemInfoContentBinding.class, null));
        binding.rvConvert.setLayoutManager(new LinearLayoutManager(mContext));
        List<InfoContent> temp2 = new ArrayList<>();
        for (QueryList.DataDTO.ItemsDTO itemsDTO : data.getItems()) {
            temp2.add(new InfoContent(itemsDTO.getSgTypeName(), itemsDTO.getMaozhong()));
        }
        binding.rvConvert.setAdapter(new ConvertAdapter(temp2, ItemConvertBinding.class));
        presenter.getNotice().observe(this, integer -> {
            if (integer == 0) {
                finish();
            }
        });
    }

    @Override
    public void initListener() {
        super.initListener();
        binding.ivBack.setOnClickListener(v -> finish());
        binding.btnConvert.setOnClickListener(v -> {
            ToConvert convert = new ToConvert();
            convert.setCzsysid(data.getCzSysid());
            List<ToConvert.ItemsDTO> items = new ArrayList<>();
            RecyclerView.Adapter adapter = binding.rvConvert.getAdapter();
            for (int i = 0; i < adapter.getItemCount(); i++) {
                RecyclerView.ViewHolder viewHolder = binding.rvConvert.findViewHolderForAdapterPosition(i);
                if (viewHolder != null) {
                    String tvOld = ((TextView) viewHolder.itemView.findViewById(R.id.tv_old)).getText().toString();
                    String tvNew = ((TextView) viewHolder.itemView.findViewById(R.id.tv_new)).getText().toString();
                    if (!StringUtils.isEmpty(tvNew)) {
                        ToConvert.ItemsDTO itemsDTO = new ToConvert.ItemsDTO();
                        itemsDTO.setOldsgtypesysid(AllTypeDataBase.getSysIdByName(tvOld));
                        itemsDTO.setNewsgtypesysid(AllTypeDataBase.getSysIdByName(tvNew));
                        items.add(itemsDTO);
                    }
                }
            }
            convert.setItems(items);
            presenter.convert(convert);
        });
    }

}
