import { type Signal, useSignal } from "@preact/signals";
import { bplan, Plan, plandb } from "../common/plan.ts";
import Calendar from "./Calendar.tsx";
import { datefrom } from "../common/date.ts";

export default function Planner() {
    const date = useSignal(self.localStorage ? parseInt(localStorage.getItem("entemu.date") || "0") : 0);
    date.subscribe(d => self.localStorage ? localStorage.setItem("entemu.date", d.toString()) : void 0);
    const plans: Signal<Plan[]> = useSignal([
        // base
        bplan(plandb.iso9000),
        bplan(plandb.iso14000),
        bplan(plandb.market_domestic),
        bplan(plandb.market_asia),
        bplan({ name: "管理费", times: 5*12*30, period: 30, price: 5 }),
        bplan({ name: "维修费", start: datefrom(2, 4, 1), times: 4, amount: 4, period: 360, price: 15 }),

        // start 1
        bplan(plandb.ads),
        bplan(plandb.strategy_ads),
        bplan(plandb.rent_factory, { times: 4, amount: 1 }),
        // bplan(plandb.p1_cert),
        bplan(plandb.p2_cert),
        bplan(plandb.p4_cert),
        bplan(plandb.build_line_auto, { amount: 4 }),
        bplan(plandb.optimize_auto, { start: datefrom(1, 4, 1), amount: 4 }),
        // bplan(plandb.loan, { price: -500, start: datefrom(1, 3, 1) }),
        // bplan(plandb.loanback, { price: 525, start: datefrom(1, 11, 1) }),
        // bplan(plandb.loan, { price: -30, start: datefrom(1, 6, 17) }),
        // bplan(plandb.loanback, { price: 31.5, start: datefrom(2, 6, 17) }),

        // bplan(plandb.loan, { price: -10, start: datefrom(1, 7, 1) }),
        // bplan(plandb.loanback, { price: 10.5, start: datefrom(2, 7, 1) }),

        // bplan(plandb.loan, { price: -40, start: datefrom(1, 8, 13) }),
        // bplan(plandb.loanback, { price: 42, start: datefrom(2, 8, 13) }),

        bplan(plandb.loan, { price: -480-360, start: datefrom(1, 12, 30) }),
        bplan(plandb.loanback, { price: 480*1.1 + 360*1.05, start: datefrom(3, 1, 1) }),

        bplan(plandb, { name: "贴现", price: 40-40*1.1, start: datefrom(2, 1, 1) }),

        bplan(plandb.p4_sell, { name: "贱卖 P4", start: datefrom(1, 11, 1), price: -50, role: "-p4" }),

        // prod 1
        // bplan(plandb.p1_produce, { start: 110, period: 56, amount: 30 }),
        bplan(plandb.p2_produce, { start: datefrom(1, 4, 21), period: 56, times: 30, amount: 3 }),
        bplan(plandb.p4_produce, { start: datefrom(1, 9, 1), period: 56, times: 30 }),

        bplan(plandb.p2_sell, { start: datefrom(1, 7, 15), amount: 3, price: 0, next: [{ name: "收到 P2 账款", price: -60, start: 30, }] }),
        bplan(plandb.p2_sell, { start: datefrom(1, 12, 21), amount: 9, price: 0, next: [{ name: "收到 P2 账款", price: -54, start: 30, }] }),

        // year 2
        bplan(plandb.ad),
        bplan(plandb.p2_sell, { start: datefrom(2, 3, 27), amount: 6, next: [{ name: "收到 P2 账款", price: -51, start: 30, }] }),
        bplan(plandb.p2_sell, { start: datefrom(2, 7, 19), amount: 6, next: [{ name: "收到 P2 账款", price: -58, start: 30, }] }),

        bplan(plandb.p4_sell, { start: datefrom(2, 2, 19), amount: 2, next: [{ name: "收到 P4 账款", price: -92, start: 30, }] }),
        bplan(plandb.p4_sell, { start: datefrom(2, 6, 11), amount: 2, next: [{ name: "收到 P4 账款", price: -91, start: 30, }] }),

        // year 3
        bplan(plandb.p2_sell, { start: datefrom(3, 11, 19), amount: 25, next: [{ name: "收到 P2 账款", price: -58, start: 30, }] }),
        bplan(plandb.p4_sell, { start: datefrom(3, 11, 19), amount: 8, next: [{ name: "收到 P4 账款", price: -92, start: 30, }] }),

        // year 4
        bplan(plandb.p2_sell, { start: datefrom(4, 12, 21), amount: 19, next: [{ name: "收到 P2 账款", price: -58, start: 30, }] }),
        bplan(plandb.p4_sell, { start: datefrom(4, 12, 21), amount: 8, next: [{ name: "收到 P4 账款", price: -92, start: 30, }] }),
    ]);
    return <div>
        <Calendar date={date} plans={plans} />
    </div>;
}
