package yem.wm.cm.csrm.dynamic;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.control.Image;
import kd.bos.form.control.Label;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import yem.base.common.utils.BigDecimalUtils;
import yem.base.common.utils.YEM;

import java.math.BigDecimal;
import java.util.Date;
import java.util.EventObject;

/**
 * 外汇牌价
 */
public class ExchangeRateEdit extends AbstractFormPlugin {

    @Override
    public void afterBindData(EventObject e) {
        initValue(this.getView());
    }

    public void initValue(IFormView view) {
        long currentOrgId = YEM.getCurrentOrgId();//当前登录组织
        //查询核算组织本位币设置  获取当前组织的本位币
        DynamicObject bd_accountingsys_base = BusinessDataServiceHelper.loadSingle("bd_accountingsys_base", "basecurrrency", new QFilter[]{new QFilter("baseacctorg", QCP.equals, currentOrgId)});
        DynamicObject baseacctorg = null;
        if (bd_accountingsys_base != null) {
            baseacctorg = bd_accountingsys_base.getDynamicObject("basecurrrency");
        } else {//没有设置当前组织的本位币，默认按照人人民币
            baseacctorg = BusinessDataServiceHelper.loadSingle("bd_currency", "id", new QFilter[]{new QFilter("number", QCP.equals, "CNY")});
        }

        this.getModel().setValue("yem_descurrency", baseacctorg.getLong("id"));
        this.getView().updateView("yem_descurrency");
        setLabel(this.getView(), "yem_labelap2", "yem_labelap4", "yem_descurrency");

        Image yem_imageap1 = view.getControl("yem_imageap1");//获取原币图片控件
        yem_imageap1.setUrl(String.format("/isv/yem/globalimgs/images/%s", baseacctorg.getString("number") + ".png"));

        DynamicObject bd_currency = BusinessDataServiceHelper.loadSingle("bd_currency", "id", new QFilter[]{new QFilter("number", QCP.equals, "USD")});
        DynamicObject pex_servicecurrency = BusinessDataServiceHelper.loadSingle("yem_pex_servicecurrency", "id", new QFilter[]{new QFilter("yem_currency", QCP.equals, bd_currency.getLong("id"))});
        this.getModel().setValue("yem_srccurrency", bd_currency.getLong("id"));
        this.getView().updateView("yem_srccurrency");

        if (pex_servicecurrency != null) {
            this.getModel().setValue("yem_currency", pex_servicecurrency.getLong("id"));
        } else {
            Image yem_imageap = view.getControl("yem_imageap");
            yem_imageap.setUrl(String.format("/isv/yem/globalimgs/images/%s", bd_currency.getString("number") + ".png"));
        }

        getCurrenyRate(this.getView(), this.getModel());
    }

    @Override
    public void registerListener(EventObject e) {
        Image yem_imageap = this.getControl("yem_imageap");
        yem_imageap.addClickListener(this);
    }

    @Override
    public void click(EventObject e) {
        Control control = (Control) e.getSource();
        String key = control.getKey();
        switch (key) {
            case "yem_imageap":
                showList(this.getView());
                break;
            default:
                break;
        }
    }

    private void showList(IFormView view) {
        BasedataEdit yem_currency = this.getControl("yem_currency");
        yem_currency.click();
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String name = e.getProperty().getName();
        BigDecimal yem_rate = (BigDecimal) this.getModel().getValue("yem_rate");
        switch (name) {
            case "yem_currency"://业务币种
                currency(this.getModel());
                break;
            case "yem_srccurrency":
                setLabel(this.getView(), "yem_labelap21", "yem_labelap41", name);
                getCurrenyRate(this.getView(), this.getModel());
                break;

            case "yem_descurrency":
                setLabel(this.getView(), "yem_labelap2", "yem_labelap4", name);
                getCurrenyRate(this.getView(), this.getModel());
                break;

            case "yem_srcamount" :
                BigDecimal yem_srcamount = (BigDecimal) this.getModel().getValue("yem_srcamount");
                this.getModel().beginInit();
                this.getModel().setValue("yem_desamount", yem_srcamount.multiply(yem_rate));
                this.getModel().endInit();
                this.getView().updateView("yem_desamount");
                break;
            case "yem_desamount":
                BigDecimal yem_desamount = (BigDecimal) this.getModel().getValue("yem_desamount");
                if(yem_rate.compareTo(BigDecimal.ZERO) > 0){
                    this.getModel().beginInit();
                    this.getModel().setValue("yem_srcamount", BigDecimalUtils.div(yem_desamount, yem_rate, 4));
                    this.getModel().endInit();
                    this.getView().updateView("yem_srcamount");
                }

                break;
            default:
                break;
        }
    }

    public void getCurrenyRate(IFormView view, IDataModel model) {
        DynamicObject yem_srccurrency = (DynamicObject) model.getValue("yem_srccurrency");
        DynamicObject yem_descurrency = (DynamicObject) model.getValue("yem_descurrency");
        DynamicObject bd_exratetable = (DynamicObject) model.getValue("yem_currencytable");
        BigDecimal rate = YEM.getExchangeRate(bd_exratetable, yem_srccurrency, yem_descurrency, new Date());
        model.setValue("yem_rate", rate);
        BigDecimal yem_srcamount = (BigDecimal) model.getValue("yem_srcamount");
        model.setValue("yem_desamount", yem_srcamount.multiply(rate));

        if (yem_srccurrency != null && yem_descurrency != null) {
            Label yem_labelap6 = view.getControl("yem_labelap6");
            BigDecimal desrate = BigDecimal.ZERO;
            if (rate.compareTo(BigDecimal.ZERO) > 0){
                desrate = BigDecimalUtils.div(BigDecimalUtils.one, rate, 4);
            }

            String labelap6 = BigDecimalUtils.one + " " + yem_descurrency.getString("name") + " ≈ " + desrate + " " + yem_srccurrency.getString("name");
            yem_labelap6.setText(labelap6);

            Label yem_labelap5 = view.getControl("yem_labelap5");
            String labelap5 = BigDecimalUtils.one + " " + yem_srccurrency.getString("name") + " ≈ " + rate.setScale(4, BigDecimal.ROUND_HALF_UP) + " " + yem_descurrency.getString("name");
            yem_labelap5.setText(labelap5);
        }
    }

    /**
     * 给标签赋值
     *
     * @param view
     * @param labelname
     * @param labelnumber
     * @param name
     */
    public void setLabel(IFormView view, String labelname, String labelnumber, String name) {
        DynamicObject currency = (DynamicObject) view.getModel().getValue(name);
        if (currency != null) {
            Label labelnameValue = view.getControl(labelname);
            labelnameValue.setText(currency.getString("name"));

            Label labelnumberValue = view.getControl(labelnumber);
            labelnumberValue.setText(currency.getString("number"));
        }

    }

    private void currency(IDataModel model) {
        DynamicObject yem_currency = (DynamicObject) model.getValue("yem_currency");
        if (yem_currency != null) {
            yem_currency = BusinessDataServiceHelper.loadSingleFromCache(yem_currency.getLong("id"), yem_currency.getDynamicObjectType().getName());
            DynamicObject currency = yem_currency.getDynamicObject("yem_currency");
            model.setValue("yem_srccurrency", currency.getLong("id"));
            Image yem_imageap = this.getView().getControl("yem_imageap");
            yem_imageap.setUrl(String.format("/isv/yem/globalimgs/images/%s", currency.getString("number") + ".png"));
        }
    }
}