package com.bbitcn.silk.ui.fragment;

import android.text.Editable;
import android.text.TextWatcher;

import com.bbitcn.silk.base.BaseFragment;
import com.bbitcn.silk.databinding.FragmentTareInfoBinding;
import com.bbitcn.silk.model.net.QueryList;
import com.bbitcn.silk.model.net.ToTareSubmit;
import com.bbitcn.silk.utils.database.TareDataBase;
import com.bbitcn.silk.utils.global.Global;
import com.blankj.utilcode.util.StringUtils;

/**
 * @Description TODO
 * @Author DuanKaiji
 * @CreateTime 2024年04月01日 10:54:37
 */
public class TareInfoFragment extends BaseFragment<FragmentTareInfoBinding, TareInfoFragmentPresenter> {
    QueryList.DataDTO.ItemsDTO data;
    String CzSysid;

    public TareInfoFragment(String CzSysid, QueryList.DataDTO.ItemsDTO data) {
        this.CzSysid = CzSysid;
        this.data = data;
    }

    @Override
    public void init() {
        ToTareSubmit.ItemsDTO catchData = TareDataBase.getCatchById(CzSysid + data.getSgTypeSysid());
        if (catchData != null) {
            //如果有缓存数据则显示缓存数据
            binding.tiSkin.setText(String.valueOf(catchData.getPizhong()));
            binding.tiTare.setText(String.valueOf(catchData.getKouzhong()));
            binding.tiPrice.setText(String.valueOf(catchData.getPrice()));
        } else {
            binding.tiSkin.setText(String.valueOf(data.getPizhong()));
            binding.tiTare.setText(String.valueOf(data.getKouzhong()));
            binding.tiPrice.setText(String.valueOf(data.getPrice()));
        }
        binding.tvPackage.setText(String.valueOf(data.getBaoshu()));
        if (data.getAllowEditPrice() == 0) {
            // 0表示不允许修改价格
            binding.tiPrice.setEnabled(false);
        }
        refreshUnit();
    }

    @Override
    public void initListener() {
        super.initListener();
        //输入皮重和扣重后即时计算净重
        TextWatcher tw = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //do nothing
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                calculateTotal();
            }

            @Override
            public void afterTextChanged(Editable s) {
                ToTareSubmit.ItemsDTO dto = new ToTareSubmit.ItemsDTO();
                dto.setSgtypesysid(CzSysid + data.getSgTypeSysid());
                dto.setPizhong(binding.tiSkin.getText().toString().isEmpty() ? 0 : Double.parseDouble(binding.tiSkin.getText().toString()) / Global.getUnitInt());
                dto.setKouzhong(binding.tiTare.getText().toString().isEmpty() ? 0 : Double.parseDouble(binding.tiTare.getText().toString()) / Global.getUnitInt());
                dto.setPrice(binding.tiPrice.getText().toString().isEmpty() ? 0 : Double.parseDouble(binding.tiPrice.getText().toString()));
                TareDataBase.updateCatch(dto);
            }
        };
        binding.tiSkin.addTextChangedListener(tw);
        binding.tiPrice.addTextChangedListener(tw);
        binding.tiTare.addTextChangedListener(tw);
        binding.tiSkin.setSelectAllOnFocus(true);
        binding.tiPrice.setSelectAllOnFocus(true);
        binding.tiTare.setSelectAllOnFocus(true);
    }

    /**
     * 计算总价
     */
    private void calculateTotal() {
        double skin = StringUtils.isEmpty(binding.tiSkin.getText().toString()) ? 0 : Double.parseDouble(binding.tiSkin.getText().toString());
        double tare = StringUtils.isEmpty(binding.tiTare.getText().toString()) ? 0 : Double.parseDouble(binding.tiTare.getText().toString());
        double gross = binding.tvGross.getText().toString().isEmpty() ? 0 : Double.parseDouble(binding.tvGross.getText().toString());
        double net = gross - skin - tare;
        double total = net * Double.parseDouble(StringUtils.isEmpty(binding.tiPrice.getText().toString()) ? "0" : binding.tiPrice.getText().toString());
        if (gross < 0 || skin < 0 || total < 0) {
            binding.tvNet.setText("数据错误");
            binding.tvTotal.setText("数据错误");
        } else {
            //将输出格式化为两位小数
            binding.tvNet.setText(String.format("%.2f", net));
            binding.tvTotal.setText(String.format("%.2f", total));
        }
    }

    public String getSkin() {
        return binding.tiSkin.getText().toString();
    }

    public String getTare() {
        return binding.tiTare.getText().toString();
    }

    public String getPrice() {
        return binding.tiPrice.getText().toString();
    }

    public String getNet() {
        return binding.tvNet.getText().toString();
    }

    public void refreshUnit() {
        binding.tvUnit.setText("（元/" + Global.getUnit() + "）");
        //刷新单位后重新显示净重与毛重.
        binding.tvGross.setText(String.valueOf(data.getMaozhong() * Global.getUnitInt()));
        binding.tvNet.setText(String.valueOf(data.getMaozhong() * Global.getUnitInt()));
        calculateTotal();
    }

}
