import io


def tourtypeday_to_tourskeleton(inst, isStringIO=True):
    """
    Use values of TourTypeDay variables to create a "tour skeleton"
    which indicates days worked and days off.

    :param inst: Model instance
    :param isStringIO: True (default) to return StringIO object, False to return string
    :return: Tour skeleton as either as a StringIO object or a string.
    """

    # 'dummy' entry in list is for column heading lineup in csv import
    header_list = ['n', 'i', 't', 'dummy']
    for w in range(1, inst.n_weeks + 1):
        for d in range(1, inst.n_days_per_week + 1):
            header_list.append(str(w) + '_' + str(d))

    param = ','.join(header_list) + '\n'

    tour_num = 0
    for (i, t) in inst.okTourType:
        if inst.TourType[i, t].value > 0:
            pos_list = [str(p) for p in (i, t)]
            tour_num += 1
            data_row = str(tour_num) + ',' + ','.join(pos_list) + ', x, '
            day_list = []
            for w in range(1, inst.n_weeks + 1):
                for d in range(1, inst.n_days_per_week + 1):
                    day_list.append(str(inst.TourTypeDay[i, t, d, w]()))

            data_row += ','.join(day_list) + '\n'
            param += data_row

    param += "\n"

    # build the header

    if isStringIO:
        param_out = io.StringIO()
        param_out.write(param)
        return param_out.getvalue()
    else:
        return param