/******************************************************************************
*	File		-	args.cpp
*	Author		-	Joey Pollack
*	Date		-	2021/09/01 (y/m/d)
*	Mod Date	-	2021/09/01 (y/m/d)
*	Description	-	Processes command line arguments.
******************************************************************************/

#include "args.h"
#include <sstream>

namespace lunarium
{
    
    Args::Args(int argc, char** argv, char switch_char, std::vector<SwitchDesc>& switches)
        : mNumArgs(argc), mSwitchChar(switch_char)
    {
        for (int i = 0; i < switches.size(); i++)
        {
            mSwitchHasValue.insert( { switches[i].symbol, switches[i].expect_value } );
        }

        for (int i = 0; i < mNumArgs; i++)
        {
            mArgs.push_back(std::string(argv[i]));
        }

        Process();
    }

    const std::vector<std::string>& Args::GetPositionalArgs() const
    {
        return mPositionalArgs;
    }

    const std::map<std::string, std::string>& Args::GetSwitchArgs() const
    {
        return mSwitchArgs;
    }

    int Args::GetNumPositionalArgs() const
    {
        return mPositionalArgs.size();
    }

    // Does no error checking.
    const std::string& Args::GetArgAtPosition(int pos) const
    {
        return mPositionalArgs[pos];
    }

    bool Args::ContainsSwitch(std::string s) const
    {
        return mSwitchArgs.find(s) != mSwitchArgs.end();
    }

    // Does no error checking.
    const std::string& Args::GetSwitchValue(std::string s) const
    {
        return mSwitchArgs.find(s)->second;
    }

    const std::vector<std::string>& Args::GetRawArgs() const
    {
        return mArgs;
    }

    void Args::Process()
    {
        for (int i = 0; i < mNumArgs; i++)
        {
            bool isSwitch = (mArgs[i][0] == mSwitchChar);
            
            if (isSwitch)
            {
                int curPos = 1;
                std::ostringstream iss;

                // This is a switch need to store everything after the switch char
                while (curPos < mArgs[i].size())
                {
                    iss << mArgs[i][curPos];
                    curPos++;
                }

                // Store the switch
                std::string skey = iss.str();
                std::string svalue = "";

                // check that this is a valid switch
                auto siter = mSwitchHasValue.find(skey);
                if (siter != mSwitchHasValue.end())
                {
                    // Need to also consume the next arg in the vector
                    // if this switch has a value
                    if (siter->second)
                    {
                        i++;
                        curPos = 0;
                        iss.str("");
                        iss.clear();

                        while (curPos < mArgs[i].size())
                        {
                            iss << mArgs[i][curPos];
                            curPos++;
                        }

                        svalue = iss.str();
                    }
                    
                    mSwitchArgs.insert( { skey, svalue } );

                }

            }
            else
            {
                mPositionalArgs.push_back(mArgs[i]);
            }
        }
    }
}