package cz.moneta.test.sanity.transactional.cards.firstvision;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.support.rest.RawRestRequest;
import cz.moneta.test.sanity.transactional.SmokeData;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE.*;

@TestScenario(name = "Pricing table")
public class PricingTable {
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1915")
    public void VMX_ACCT_PRICING_CTRL_UPD_EMEA(Harness harness) {
        String account = SmokeData.getInstance(harness).getCreditData().getAccount();

        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 1);
        Date date = c.getTime();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(date);
        c.add(Calendar.MONTH, 1);
        date = c.getTime();
        String expireDate = dateFormat.format(date);
        RawRestRequest.Post request = harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.ACCT.PRICING.CTRL.UPD.EMEA", "E8V1"))
                .addField(VMX_MSGIN, "STATE_OF_RESID", "T04")
                .addField(VMX_MSGIN, "PCT_CTL_ID", "R04")
                .addField(VMX_MSGIN, "PCT_START_DATE", startDate)
                .addField(VMX_MSGIN, "PCT_EXPIRE_DATE", expireDate)
                .addField(VMX_MSGIN, "ACCOUNT", account);

        RawRestRequest.Response response = request.postExtended();

        if (response.extract(SVC_RETURN).textValue().equals("F")) {
            request.addField(VMX_MSGIN, "STATE_OF_RESID", "W04")
                    .postExtended();
        }

        response.andAssertFieldValue(SVC_RETURN, "P")
                .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT_DATA.PCT_CTL_ID", "R04");
    }
}
