package usda.weru.weps.wepsRunControl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import usda.weru.util.WepsMessage;
import usda.weru.util.WepsMessageLog;

/**
 *
 * @author mhaas
 */
public class WrcPipeIn extends Thread {

        private final WepsMessageLog c_log;
        InputStream from;
        PrintWriter to;
        private final WrcBackgroundIf dlgIf;

        public WrcPipeIn(InputStream from, PrintWriter to, WrcBackgroundIf dlgIf, int idx) {
            this.from = from;
            this.to = to;
            this.dlgIf = dlgIf;

            c_log = new WepsMessageLog();
        }

        public WepsMessageLog getLog() {
            return c_log;
        }

        @Override
        public void run() {

            String inpstr, trimstr, message = null;
            BufferedReader bid = new BufferedReader(new InputStreamReader(from), 10);

            try {
                while ((inpstr = bid.readLine()) != null) {
                    to.println(inpstr);
                    trimstr = inpstr.trim();

                    //Special run time catches
                    //if (rp != null) {
                    if (dlgIf != null) {
                        //trim the input string so that the leading white spaces are removed-neha
                        if (
                        	trimstr.startsWith("Year")  // single subregion WEPS
                        	||
                        	trimstr.startsWith("Subregion            1 Year") // Mulit-subregion WEPS
                        ) {
                            dlgIf.bkgndDlgWriteStatus(inpstr);
                            StringTokenizer st = new StringTokenizer(inpstr);
                            st.nextToken();
                            dlgIf.bkgndDlgSetPbarCurrent(Integer.parseInt(st.nextToken()));
                        }
                    }
                    //Catch Errors and warnings
                    if (trimstr.startsWith("Requested Station Not Found.")) {
                        //Cligen was not happy, unable to find desired station.
                        c_log.logMessage(WepsMessage.errorMessage(
                                "Selected cligen station does not exist in the cligen database being used."));
                    } else if (trimstr.startsWith("Error")) {
                        message = trimstr.replaceFirst("Error:", "");
                        message = message.replaceFirst("Error :", "");
                        c_log.logMessage(WepsMessage.errorMessage(message.trim()));
                    } else if (trimstr.startsWith("Warning")) {
                        message = trimstr.replaceFirst("Warning:", "");
                        message = message.replaceFirst("Warning :", "");
                        c_log.logMessage(WepsMessage.warningMessage(message.trim()));
                    }

                }
                to.flush();
            } catch (IOException e) {
                //System.err.println("PipeIn: " + e);
            }
        }
    
}
