package com.crm.custom.controller;

import com.crm.custom.service.*;
import com.crm.login.rely.backend.controller.BaseLoginController;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.model.entity.account.apply.password.reset.AccountApplyPasswordResetAddEntity;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.pojo.table.CustomLoginAssetManagementTable;
import com.crm.rely.backend.model.pojo.table.CustomLoginDemoTable;
import com.crm.rely.backend.model.pojo.table.CustomLoginFirmOfferTable;
import com.crm.rely.backend.model.pojo.view.CustomLoginView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/account/password/reset")
public class AccountApplyPasswordResetController extends BaseLoginController {

    @Autowired
    private AccountApplyPasswordResetService service;

    @Autowired
    private CustomLoginDemoService customLoginDemoService;

    @Autowired
    private CustomLoginFirmOfferService customLoginFirmOfferService;

    @Autowired
    private CustomLoginAssetManagementService customLoginAssetManagementService;

    @Autowired
    private CustomLoginService customLoginService;

    @PostMapping("/apply")
    public BaseResultDto<Object> customAdd(@RequestBody @Validated AccountApplyPasswordResetAddEntity entity,
                                              CustomInfoEntity infoEntity) throws Exception {

        switch (entity.getLoginType()) {
            case 1:
                CustomLoginDemoTable customLoginDemoTable = customLoginDemoService.getByLogin(entity.getLogin());
                entity.setCustomId(customLoginDemoTable.getCustomId());
                entity.setCId(customLoginDemoTable.getCId());
                break;
            case 2:
                CustomLoginFirmOfferTable customLoginFirmOfferServiceByLogin = customLoginFirmOfferService.getByLogin(entity.getLogin());
                entity.setCustomId(customLoginFirmOfferServiceByLogin.getCustomId());
                entity.setCId(customLoginFirmOfferServiceByLogin.getCId());
                break;
            case 3:
                CustomLoginAssetManagementTable customLoginAssetManagementTable = customLoginAssetManagementService.getByLogin(entity.getLogin());
                entity.setCustomId(customLoginAssetManagementTable.getCustomId());
                entity.setCId(customLoginAssetManagementTable.getCId());
                break;
            case 4:
                CustomLoginView customLoginView = customLoginService.getByLogin(entity.getLogin());
                entity.setCustomId(customLoginView.getCustomId());
                entity.setCId(customLoginView.getCId());
                break;
        }

        if (infoEntity.getId().equals(entity.getCustomId())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }

        service.addAccountApply(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/add")
    @FeignClientAnnotation
    public BaseResultDto<Object> managerAdd(@RequestBody @Validated AccountApplyPasswordResetAddEntity entity) throws Exception {


        switch (entity.getLoginType()) {
            case 1:
                CustomLoginDemoTable customLoginDemoTable = customLoginDemoService.getByLogin(entity.getLogin());
                entity.setCustomId(customLoginDemoTable.getCustomId());
                entity.setCId(customLoginDemoTable.getCId());
                break;
            case 2:
                CustomLoginFirmOfferTable customLoginFirmOfferServiceByLogin = customLoginFirmOfferService.getByLogin(entity.getLogin());
                entity.setCustomId(customLoginFirmOfferServiceByLogin.getCustomId());
                entity.setCId(customLoginFirmOfferServiceByLogin.getCId());
                break;
            case 3:
                CustomLoginAssetManagementTable customLoginAssetManagementTable = customLoginAssetManagementService.getByLogin(entity.getLogin());
                entity.setCustomId(customLoginAssetManagementTable.getCustomId());
                entity.setCId(customLoginAssetManagementTable.getCId());
                break;
            case 4:
                CustomLoginView customLoginView = customLoginService.getByLogin(entity.getLogin());
                entity.setCustomId(customLoginView.getCustomId());
                entity.setCId(customLoginView.getCId());
                break;
        }

        service.addAccountApply(entity);
        return BaseResultDto.success();
    }

}
