/*
 * Copyright (c) 2016 Route1 Inc.  All rights reerved.  <sales@route1.com>
 * http://www.route1.com
 *
 * You may use, distribute and modify this code under the
 * terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This code is provided “as -is,” without warranty of any kind,
 * express, implied or statutory, including but not limited to
 * any implied warranties or conditions of quality, performance,
 * merchantability, merchantable quality, durability,
 * non-infringement or of fitness for a particular purpose.
 * See the GPLv2 for more details.
 *
 * Contributor:  A. M. Jehad Sarkar  <amjehad.sarkar@route1.com>
*/

package com.routeone.dc.vpn;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.ProgressBar;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.activities.BaseActivity;
import de.blinkt.openvpn.activities.MainActivity;
import de.blinkt.openvpn.core.ConfigParser;
import de.blinkt.openvpn.core.ProfileManager;
import de.blinkt.openvpn.fragments.VPNProfileList;

public class Route1ConfigConverter extends BaseActivity{
    public static final String IMPORT_PROFILE = "com.routeone.dc.vpn.IMPORT_PROFILE";
    private String mData;
    private AsyncTask<Void, Void, Integer> mImportTask;
    private VpnProfile mResult;

    public static final String configName = "DerivIDCPConfiguration";

    private void startImportTask(final String data) {
        mImportTask = new AsyncTask<Void, Void, Integer>() {
            private ProgressBar mProgress;

            @Override
            protected void onPreExecute() {
            }

            @Override
            protected Integer doInBackground(Void... params) {
                try {
                    InputStream is = new ByteArrayInputStream(data.getBytes());

                    doImport(is, getApplicationContext());
                    if (mResult==null)
                        return -3;
                } catch (SecurityException se){
                    return -2;
                }
                return 0;
            }

            @Override
            protected void onPostExecute(Integer errorCode) {

                if (errorCode == 0) {
                    mResult.mName = configName;
                }
            }
        }.execute();
    }
    private void doImport(InputStream is, Context context) {
        ConfigParser cp = new ConfigParser();
        try {
            InputStreamReader isr = new InputStreamReader(is);
            cp.parseConfig(isr);
            mResult = cp.convertRoute1Profile(context);
            return;

        } catch (IOException | ConfigParser.ConfigParseError e) {
        }
        mResult = null;

    }

    public void saveProfile(ProfileManager vpl, Context context, String data) {
        InputStream is = new ByteArrayInputStream(data.getBytes());
        doImport(is, context);
        if (mResult!=null) {
            mResult.mName = configName;
            mResult.mProfileCreator = "Route1";
            VpnProfile vp = vpl.getProfileByName(configName);
            if(vp != null) {
                mResult.setUUID(vp.getUUID());
                vpl.removeProfile(context, vp);
                vpl.saveProfileList(context);
            }
            vpl.addProfile(mResult);
            vpl.saveProfile(context, mResult);
            vpl.saveProfileList(context);
        }
        //String temp = mResult.getUUID().toString();
        //startActivity(getIntent());
        //finish();
        Intent intent = new Intent(context,MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
    }
}
