<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def handle_segment_ended(self):
        '''
            process end of the segment based on template
        '''
        if self._state[1].value.endevent is not None:
            self.events.append((self._state[1].value.endevent, self._state[0], None))
        if self._state[1].value.multiplier is 2:
            self.parentismap = 0
            self.waitingforprop = 0
        if(len(self._stack) is  0):
            self._scstate = ScannerState.IDLE            
            return       
        if self._state[1].value.valuetype is not ValueType.RAW:
            self._stack[-1][3] = self._stack[-1][3] - 1  # #???
        if self._stack[-1][3] is 0:
            self._scstate = ScannerState.SEGMENT_ENDED
            self._state = self._stack.pop()  # pop last state from stack
            if self._state[1].value.multiplier is 2:
                self.parentismap = 1
                self.waitingforprop = 1
            self.handle_segment_ended()
        else:
            if self._stack[-1][1].value.multiplier is 2:
                self.parentismap = 1
                self.waitingforprop = 1
            self._scstate = ScannerState.WAITING_FOR_HEADER