1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use chain_core::property::BlockDate as _;
use chain_impl_mockchain::{
    block::BlockDate,
    certificate::{ExternalProposalId, Proposal, Proposals, VoteAction, VotePlan},
    testing::VoteTestGen,
    tokens::identifier::TokenIdentifier,
    vote::{Options, PayloadType},
};
use chain_vote::MemberPublicKey;
use std::str::FromStr;
pub struct VotePlanBuilder {
    proposals_builder: ProposalsBuilder,
    action: VoteAction,
    payload: PayloadType,
    member_keys: Vec<MemberPublicKey>,
    vote_start: BlockDate,
    tally_start: BlockDate,
    tally_end: BlockDate,
    voting_token: TokenIdentifier,
    options_size: u8,
}

impl Default for VotePlanBuilder {
    fn default() -> Self {
        VotePlanBuilder::new()
    }
}

impl VotePlanBuilder {
    pub fn new() -> Self {
        Self {
            proposals_builder: ProposalsBuilder::default().with_count(3),
            options_size: 3,
            action: VoteAction::OffChain,
            payload: PayloadType::Public,
            member_keys: Vec::new(),
            vote_start: BlockDate::from_epoch_slot_id(0, 0),
            tally_start: BlockDate::from_epoch_slot_id(1, 0),
            tally_end: BlockDate::from_epoch_slot_id(2, 0),
            voting_token: TokenIdentifier::from_str(
                "00000000000000000000000000000000000000000000000000000000.00000000",
            )
            .unwrap(),
        }
    }

    pub fn proposals_count(mut self, proposals_count: usize) -> Self {
        self.proposals_builder = self.proposals_builder.with_count(proposals_count);
        self
    }

    pub fn proposals_external_ids(mut self, proposals_ids: Vec<ExternalProposalId>) -> Self {
        self.proposals_builder = self.proposals_builder.with_ids(proposals_ids);
        self
    }

    pub fn action_type(mut self, action: VoteAction) -> Self {
        self.action = action;
        self
    }

    pub fn private(mut self) -> Self {
        self.payload = PayloadType::Private;
        self
    }

    pub fn public(mut self) -> Self {
        self.payload = PayloadType::Public;
        self
    }

    pub fn member_public_key(mut self, key: MemberPublicKey) -> Self {
        self.member_keys.push(key);
        self
    }

    pub fn member_public_keys(mut self, keys: Vec<MemberPublicKey>) -> Self {
        for key in keys {
            self = self.member_public_key(key);
        }
        self
    }

    pub fn vote_start(mut self, block_date: BlockDate) -> Self {
        self.vote_start = block_date;
        self
    }

    pub fn tally_start(mut self, block_date: BlockDate) -> Self {
        self.tally_start = block_date;
        self
    }

    pub fn tally_end(mut self, block_date: BlockDate) -> Self {
        self.tally_end = block_date;
        self
    }

    pub fn voting_token(mut self, voting_token: TokenIdentifier) -> Self {
        self.voting_token = voting_token;
        self
    }

    pub fn options_size(mut self, size: u8) -> Self {
        self.options_size = size;
        self
    }

    pub fn build(self) -> VotePlan {
        let proposals = self.proposals_builder.build(self.options_size, self.action);

        VotePlan::new(
            self.vote_start,
            self.tally_start,
            self.tally_end,
            proposals,
            self.payload,
            self.member_keys.clone(),
            self.voting_token,
        )
    }
}

#[derive(Debug, Default)]
pub struct ProposalsBuilder {
    count: usize,
    external_ids: Vec<ExternalProposalId>,
}

impl ProposalsBuilder {
    pub fn with_count(mut self, count: usize) -> Self {
        self.count = count;
        self
    }

    pub fn with_ids(mut self, external_ids: Vec<ExternalProposalId>) -> Self {
        self.external_ids = external_ids;
        self
    }

    pub fn build(self, options_size: u8, action: VoteAction) -> Proposals {
        let proposals_vec: Vec<Proposal> = if !self.external_ids.is_empty() {
            self.external_ids
                .into_iter()
                .map(|epi| {
                    Proposal::new(
                        epi,
                        Options::new_length(options_size).unwrap(),
                        action.clone(),
                    )
                })
                .collect()
        } else {
            std::iter::from_fn(|| {
                Some(Proposal::new(
                    VoteTestGen::external_proposal_id(),
                    Options::new_length(options_size).unwrap(),
                    action.clone(),
                ))
            })
            .take(self.count)
            .collect()
        };
        let mut proposals = Proposals::new();
        for proposal in proposals_vec {
            let _ = proposals.push(proposal);
        }
        proposals
    }
}