package com.xiaomai.cases.polar.reserve.personal;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaomai.basetest.BaseTestImpl;
import com.xiaomai.cases.polar.reserve.ReserveTools;
import com.xiaomai.enums.ApiModule;
import com.xiaomai.enums.LoginAccount;
import com.xiaomai.enums.Terminal;
import com.xiaomai.utils.TimeUtils;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @BelongsProject: xm-sportstest
 * @BelongsPackage: com.xiaomai.cases.polar.reserve.personal
 * @Author: xuyamei
 * @CreateTime: 2024-07-24  16:18
 * @Description: 获取教练的历史特殊日期
 * @Version: 1.0
 */
public class TestGetCoachHistorySpecialDate extends BaseTestImpl {
    @Resource(name = "reserveTools")
    ReserveTools reserveTools;

    private String coachedId;

    @BeforeMethod
    @Override
    public void beforeTest(){
        setTestInfo(ApiModule.Polar_Reserve,"API_getCoachHistorySpecialDate", LoginAccount.GYM_PROD, Terminal.B,"xym");
        super.beforeTest();
    }


    @Test(description = "设置全天休息-->获取时间")
    public void testGetCoachHistorySpecialDate() {
        // 获取教练列表
        JSONObject pageStudioPersonalCoach = reserveTools.pageStudioPersonalCoach("");
        int total = pageStudioPersonalCoach.getInteger("total");
        Random rand = new Random();
        // 随机取1个教练
        coachedId = pageStudioPersonalCoach.getJSONArray("records").getJSONObject(rand.nextInt(total>10?10:total)).getString("id");

        // 获取教练后先清除特殊日期数据。避免重叠
        // 查询特殊可约时间列表
        JSONArray specialTimeVOS = reserveTools.getCoachHistorySpecialDate(coachedId);

        for (int i = 0; i < specialTimeVOS.size(); i++){
            String id = specialTimeVOS.getJSONObject(i).getJSONArray("recordIds").getString(0);
            // 删除查询到的特殊可约时间
            reserveTools.deleteSpecialDate(id);
        }

        Long date = TimeUtils.getBeforeDayDate(1);
        JSONObject body = new JSONObject();
        body.put("brandId", xmAppApi.getLoginInfo().getBrandId());
        body.put("studioId", xmAppApi.getLoginInfo().getStudioId());
        body.put("coachId",  coachedId);
        // 可约时间类型
        JSONObject specialTimeInfoRequest = new JSONObject();
        specialTimeInfoRequest.put("dutyType", "OFF_DUTY"); // 全天休息

        // 设置特定日期
        JSONArray specialTimeInfoRequestArray = new JSONArray();
        specialTimeInfoRequestArray.add(date);
        specialTimeInfoRequest.put("selectedDates", specialTimeInfoRequestArray);

        body.put("specialTimeInfoRequest", specialTimeInfoRequest);
        reserveTools.createSpecialDate(coachedId,"OFF_DUTY",specialTimeInfoRequest);

        // 设置全天休息完成后检查个人时间是否有这条数据
        specialTimeVOS = reserveTools.getCoachHistorySpecialDate(coachedId);
        boolean flag = false;
        for (int i = 0; i < specialTimeVOS.size(); i++){
            JSONObject specialTimeVO = specialTimeVOS.getJSONObject(i);
            if (date.toString().equals(specialTimeVO.getString("dutyDate"))){
                flag = true;
                Assert.assertEquals(specialTimeVO.getString("dutyType"),"-1","特殊日期类型错误");
            }
        }
        Assert.assertTrue(flag,"设置特殊日期全天休息后在历史特定日期可约时间列表没有找到相关数据");
    }

    @Test(description = "删除特殊日期",priority = 1)
    public void testDeleteSpecialDate() {
        // 查询特殊可约时间列表
        JSONArray specialTimeVOS = reserveTools.getCoachHistorySpecialDate(coachedId);

        for (int i = 0; i < specialTimeVOS.size(); i++){
            String id = specialTimeVOS.getJSONObject(i).getJSONArray("recordIds").getString(0);
            // 删除查询到的特殊可约时间
            reserveTools.deleteSpecialDate(id);
        }

        specialTimeVOS = reserveTools.getCoachHistorySpecialDate(coachedId);
        Assert.assertEquals(specialTimeVOS.size(),0,"删除特殊日期后，特殊日期列表不为空");

    }

    @Test(description = "设置特定日期可约",priority = 2)
    public void testCreateSpecialDate_ON() {
        Long date = TimeUtils.getBeforeDayDate(2);

        JSONObject specialTimeInfoRequest = new JSONObject();
        // 可约时间类型
        specialTimeInfoRequest.put("dutyType", "ON_DUTY");

        // 设置特定日期
        JSONArray selectedDates = new JSONArray();
        selectedDates.add(date);
        specialTimeInfoRequest.put("selectedDates", selectedDates);

        // 如果是可约时间非全天休息，则传可约时间段
        // 时间段 00:00 ~ 23:59
        JSONArray timePeriodInfos = new JSONArray();
        JSONObject timeInfo = new JSONObject();
        timeInfo.put("minuteOffset", 0);
        timeInfo.put("spanMinutes", 1380);
        timePeriodInfos.add(timeInfo);
        specialTimeInfoRequest.put("timePeriodInfos", timePeriodInfos);

        reserveTools.createSpecialDate(coachedId,"ON_DUTY",specialTimeInfoRequest);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        // 设置特殊可约时间后检查个人私教时间是否有这条数据
        JSONArray specialTimeVOS = reserveTools.getCoachHistorySpecialDate(coachedId);
        boolean flag = false;
        for (int i = 0; i < specialTimeVOS.size(); i++){
            JSONObject specialTimeVO = specialTimeVOS.getJSONObject(i);
            if (selectedDates.getString(0).equals(specialTimeVO.getString("dutyDate"))){
                flag = true;
                Assert.assertEquals(specialTimeVO.getString("dutyType"),"1","特殊日期类型错误");
                Assert.assertTrue(specialTimeVO.getString("timePeriodInfos").equals(timePeriodInfos.toString()),"特殊日期可约时间段");

            }
        }
        Assert.assertTrue(flag,"设置特殊日期在历史特定日期可约时间列表没有找到相关数据");
    }

    @Test(description = "删除特殊日期",priority = 3)
    public void testDeleteSpecialDate2() {
        // 查询特殊可约时间列表
        JSONArray specialTimeVOS = reserveTools.getCoachHistorySpecialDate(coachedId);

        for (int i = 0; i < specialTimeVOS.size(); i++){
            String id = specialTimeVOS.getJSONObject(i).getJSONArray("recordIds").getString(0);
            // 删除查询到的特殊可约时间
            reserveTools.deleteSpecialDate(id);
        }

        specialTimeVOS = reserveTools.getCoachHistorySpecialDate(coachedId);
        Assert.assertEquals(specialTimeVOS.size(),0,"删除特殊日期后，特殊日期列表不为空");

    }
}
