package cn.vscoder.experiment_3.rent;

import cn.vscoder.experiment_3.book.RentBook;
import cn.vscoder.experiment_3.common.PayException;
import cn.vscoder.experiment_3.reader.VIPReader;

import java.util.ArrayList;
import java.util.LinkedList;

public class RentBookManage {
    //租阅期限deadTime
    int deadTime = 30;
    double normalRent = 0.5;//正常租阅费用
    double delayRent = 1.0;//超期租阅费用
    int rentDays;
    double rent;//租阅费用

    public ArrayList<VIPReader> readerlist;//读者列表
    LinkedList<RentBook> booklist;//图书列表
    ArrayList<String> rentlist;//租阅记录
    VIPReader renter;//借阅者
    RentBook rentbook;

    public RentBookManage() {//创建空的列表
        booklist = new LinkedList<>();
        readerlist = new ArrayList<>();
        rentlist = new ArrayList<>();
    }

    //图书管理相关功能、添加图书、删除图书、打印图书列表、修改图书价格、查找图书
    public void addBook(RentBook rentBook) {
        booklist.add(rentBook);
    }

    public void addBook(String isbn, String bookname, String author, String publisher, double price, String inNo) {
        booklist.add(new RentBook(isbn, bookname, author, publisher, price, inNo));
    }

    public void addBook(int i, RentBook rentBook) {//指定位置，添加图书
        booklist.add(i - 1, rentBook);
    }

    public RentBook searchBook(String bookName) {//根据书名查找图书
        for (int j = 0; j < booklist.size(); j++) {
            if (booklist.get(j).getBookName().equals(bookName)) {
                System.out.println(booklist.get(j));
                return booklist.get(j);
            }
        }
        System.out.println("您要查找的图书：" + bookName + "不存在！");
        return null;

    }

    public void editBook(String bookName, double newprice) {
        for (int j = 0; j < booklist.size(); j++) {
            if (booklist.get(j).getBookName().equals(bookName)) {
                booklist.get(j).setPrice(newprice);
                System.out.println(booklist.get(j));
                return;
            }
        }
        System.out.println("您要查找的图书：" + bookName + "不存在！");

    }

    public void deleteBook(String bookName) {
        for (int j = 0; j < booklist.size(); j++) {
            if (booklist.get(j).getBookName().equals(bookName)) {
                booklist.remove(j);
                System.out.println("删除" + bookName + "成功！");
                return;
            }
        }
        System.out.println("您要删除的图书：" + bookName + "不存在！");

    }

    public void displayAllBooks() {
        for (RentBook rentBook : booklist) {
            System.out.println(rentBook);
        }
    }

    //读者，添加读者、显示所有读者
    public void addReader(String name, String grade) {
        readerlist.add(new VIPReader(name, grade));
    }

    public void addReader(VIPReader vipReader) {
        readerlist.add(vipReader);
    }

    public void displayAllReaders() {
        for (VIPReader vipReader : readerlist) {
            System.out.println(vipReader);
        }
    }

    //计算租金
    public double setRent() {
        if (rentDays <= deadTime) {
            rent = rentDays * normalRent * renter.getPercent();
        } else {
            rent = ((rentDays - deadTime) * delayRent + normalRent * deadTime) * renter.getPercent();
        }
        return rent;
    }

    //支付租金
    public boolean renting() throws PayException {
        setRent();
        if (renter.payRent(rent)) {
            System.out.println(renter.getReaderName() + "支付租金：" + rent + "成功！");
            return true;
        } else {
            System.out.println(renter.getReaderName() + "的账号余额为：" + renter.getBalance() + "不足以支付租金" + rent);
            return false;
        }
    }

    //租阅功能
    public boolean rentBook(VIPReader vr, RentBook bk, int days) throws PayException {
        rentbook = bk;
        renter = vr;
        rentDays = days;
        if (renting()) {
            renter.setBonusPoints(rentDays);//给借阅者积分
            rentlist.add("读者：" + renter.getReaderName() + "\t读者等级：" + renter.getReaderGrade() + "\t借阅图书为：" + bk.getBookName() + "\t租书天数：" + rentDays + "\t租金:" + rent + "读者当前积分" + renter.getBonusPoints() + "\t读者账户余额" + renter.getBalance());
            return true;
        } else {
            System.out.println("支付失败");
            return false;
        }
    }

    //展示借阅记录
    public void printRentlist() {
        for (String str : rentlist) {
            System.out.println(str);
        }
    }

    public double getNormalRent() {
        return normalRent;
    }

    public void setNormalRent(double normalRent) {
        this.normalRent = normalRent;
    }

    public double getDelayRent() {
        return delayRent;
    }

    public void setDelayRent(double delayRent) {
        this.delayRent = delayRent;
    }

    public int getDeadTime() {
        return deadTime;
    }
}
