package com.sf._04_set_contact.demo2;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class ExchangeManager {
    // 存储兑换记录信息集合
    static HashSet<ExchangeRecord> records = new HashSet<>();
    // 存储所有会员信息
    static List<Member> members = new ArrayList<>();


    /**
     * 交换商品方法
     *
     */
    public static void exchangeGoods(Member member,ExchangeGoods goods,String date){
        // 1 判断会员是否存在
        if(!members.contains(member)){
            System.out.println("会员不存在");
            return;
        }
        // 2 获取会员积分,和兑换商品积分进行比较
        int leftPoints = member.getLeftPoints();
        int points = goods.getPoints();
        // 3 如果积分不足提醒积分不足
        if(leftPoints < points){
            System.out.println("积分不足");
            return;
        }
        // 4 如果积分充足,创建兑换记录对象
        ExchangeRecord exchangeRecord = new ExchangeRecord(member,goods,date);
        // 5 把兑换记录添加set集合中
        //exchangeRecords 兑换记录  (member(1,zhangsan,3000),exchanggood(1,玩具,1000),date)
        // 判断用户之前是否已经兑换了商品
        boolean flag = isMemberExchangeGood(goods,member);
        if(!flag){
            // 6 如果添加成功,提醒添加成功,扣减积分
            // 修改set 集合当中积分  , set 集合没有获取单个数据方法也没有修改方法
            // set 当中数据存储会员信息 可以进行去重操作 最简单方式就是把set 改成list
            // memeber   List
            // {id,name,剩余积分}     List( {1,zhangsan,3000} , {2,lisi,3000})
            // 找到对应集合当中会员扣减相应积分
            /**
             * 1 如何找到对应会员信息  需要什么信息?   memberId
             * 2 找到了扣减应该扣减多少需要什么信息?   goods.getPoints();
             */
            changePoints(member.getId(),goods.getPoints());
            // 添加兑换记录
            records.add(exchangeRecord);
            System.out.println("恭喜"+member.getName()+"兑换成功"+goods.getName() +"扣减了"+ goods.getPoints());
        }else{
            // 7 如果添加失败,提醒已经兑换过次商品了
            System.out.println("之前您已经对话过这个商品了请不要重复兑换");
        }
    }

    /**
     *
     * 判断会员是否已经兑换了商品
     * set({(1,zhangsan,2000),(1,玩具,1000),2026/1/14},{(1,zhangsan,2000),(1,玩具,1000),2026/1/14})
     * @param goods   (1,玩具,1000)
     * @param member  (1,zhangsan,2000)
     * @return  如果是true 表示之前兑换过,如果是false 表示之前没有兑换过
     */
    private static boolean isMemberExchangeGood(ExchangeGoods goods, Member member) {
        boolean flag = false;
        for (ExchangeRecord record : records) {
            Member recordMember = record.getMember();
            ExchangeGoods recordExchangeGoods = record.getExchangeGoods();
            // 判断此次操作会员id 如果和兑换记录id 想通 并且 兑换商品名称也和兑换记录商品名称相同
            if(member.getId().equals(recordMember.getId()) &&
                    recordExchangeGoods.getName().equals(goods.getName())){
                return true;
            }
        }
        return flag;
    }

    private static void changePoints(Long id, int points) {
        // memberId 会员id   扣减积分
        // 找到会员   list
        for (int i = 0; i < members.size(); i++) {
            Member member = members.get(i);
            // 这个是要修改会员信息
            if(member.getId().equals(id)){
                // 修改积分 用会员剩余积分-兑换商品积分
                member.setLeftPoints(member.getLeftPoints()-points);
                // 重新设置到集合中
                members.set(i,member);
            }
        }
    }


    public static void main(String[] args) {
        // 创建3 个要兑换商品
        ExchangeGoods goods1 = new ExchangeGoods("01","玩偶",1000);
        ExchangeGoods goods2 = new ExchangeGoods("02","玩具",1000);
        ExchangeGoods goods3 = new ExchangeGoods("03","水彩笔",1000);

        // 创建会员信息
        Member member = new Member(1l,"zhangsan",3000);
        Member member1 = new Member(2l,"lisi",3000);
        // 把这两个会员添加set 集合中
        members.add(member);
        members.add(member1);

        exchangeGoods(member,goods1,"2026/1/13");
        exchangeGoods(member,goods1,"2026/1/13");
        exchangeGoods(member1,goods1,"2026/1/13");

//        for (Member m : members) {
//            System.out.println(m);
//        }
        printExchangeRecords();

        queryExchangeGoodsCount("01");
    }

    /**
     * 根据商品编码查询商品兑换了多少次
     * 核心思路遍历看比较商品编码如果相同就让count++
     */
    public static void queryExchangeGoodsCount(String code){
        // 遍历兑换记录  比较商品编码是否相同
        int count = 0;
        for (ExchangeRecord record : records) {
            if(code.equals(record.getExchangeGoods().getCode())){
                count++;
            }
        }
        System.out.println("code :"+ code+"商品兑换了:"+ count+" 次");
    }


    /**
     * 打印所有兑换记录信息
     */
    public static void printExchangeRecords(){
        for (ExchangeRecord record : records) {
            System.out.println("兑换人:"+ record.getMember().getName()+"兑换了商品:"+ record.getExchangeGoods().getName());
        }
    }
}
