package com.xixun.communicate;

import android.content.Context;

import com.xixun.display.MyLog;
import com.xixun.display.MyService;

import java.io.File;

public class WorkParams {
    public int iLightValue=LedCardCommunication.LIGHT_START_VALUE;

    public int iDirection=1;
    public int iCarriageNumber=0;
    public String strStationNumber="G1234";


    public String strProgramStart="strProgramStart";
    private Configurations configurations=null;

    MyService ms=null;

    public WorkParams(MyService service, Context ctx){
        ms = service;
        configurations = Configurations.getConfigurations(ctx);

        if (strProgramStart.equals(configurations.getValue("strProgramStart"))==false) {
            MyLog.i(Thread.currentThread().getStackTrace()[2].getFileName() + "-->"
                            + "Function: " + Thread.currentThread().getStackTrace()[2].getMethodName() + "-->"
                            + "Line" + Thread.currentThread().getStackTrace()[2].getLineNumber()
                            + "    First initial work paramters.");

            pushBaseParams(); // 第一次运行。
        }
        else pullBaseParams();
    }

    public void pushBaseParams(){
        configurations.setValue("strProgramStart", strProgramStart);
        configurations.setIntValue("iLightValue", iLightValue);

        if (ms.bDisplay_RailWay) {
            configurations.setIntValue("iDirection", iDirection);
            configurations.setIntValue("iCarriageNumber", iCarriageNumber);
            configurations.setValue("strStationNumber", strStationNumber);
        }
    }

    public void pullBaseParams(){
        iLightValue = configurations.getIntValue("iLightValue");

        if (ms.bDisplay_RailWay) {
            iDirection = configurations.getIntValue("iDirection");
            iCarriageNumber = configurations.getIntValue("iCarriageNumber");
            strStationNumber = configurations.getValue("strStationNumber");
        }
    }

    public void Backup(String strDirectPath)
    {
        String strSoureceFile=ms.GetAppPath()
                + "/shared_prefs/"
                + Configurations.strConfigFileName
                + ".xml";
        String strDirectFile=strDirectPath
                + Configurations.strConfigFileName
                + ".xml";

        //MyLog.i("strSoureceFile= " +strSoureceFile);
        //MyLog.i("strDirectFile= " +strDirectFile);

        pushBaseParams();
        FileOperator.copyFile(strSoureceFile, strDirectFile);
    }


    public void Recover(String strDirectPath)
    {
        String strDirectFile =ms.GetAppPath()
                + "/shared_prefs/"
                + Configurations.strConfigFileName
                + ".xml";
        String strSoureceFile =strDirectPath
                + Configurations.strConfigFileName
                + ".xml";

        File file=new File(strSoureceFile);
        if (file.exists()==false) return;

        //MyLog.i("strSoureceFile= " +strSoureceFile);
        //MyLog.i("strDirectFile= " +strDirectFile);

        FileOperator.copyFile(strSoureceFile, strDirectFile);

        pullBaseParams();
    }


    // /data/data/com.xixun.display/shared_prefs/DisplayAppConfig.xml
}
